Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:26:30+00:00 2026-06-01T08:26:30+00:00

MyActivity implements a CustomListener defined in MyClass.java. I have a function defined in MyClass

  • 0

MyActivity implements a CustomListener defined in MyClass.java. I have a function defined in MyClass that should trigger the listener and do some action(finish() MyActivity) defined in the activity. Somehow, I am getting null reference to the listener. Where am I going wrong?

Please refer to the code below!

MyActivity.java

import com.project.MyClass.CustomListener;

public class MyActivity extends Activity implements CustomListener {

    Context context;
    CustomListener listener;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        context = this;

        myClass = new MyClass(context);
        myClass.setOnCustomListener(listener);

    }

    public void doThisWhenTriggered()
    {
    ...
    }

}

MyClass.java

public class MyClass{

    private Contect ctx;
    ArrayList<CustomListener> listeners = new ArrayList<CustomListener>();

    public MyClass(Context context)
    {
        super();
        this.ctx = context
    }

public interface CustomListener
{
       public void doThisWhenTriggered();
}


public void setOnCustomListener(CustomListenerListener listener)
{
    this.listeners.add(listener);
}

    public void generateTrigger()
    {
        CustomListener listener = listeners.get(0);

        if(listener != null)
            listener.doThisWhenTriggered();
        else
            Log.d("MyAPP", "Listener is NULL");
    }

}

MyBroadcastReceiver.java

public void onReceive(Context context, Intent intent) {
    ...
    callMyClass(Context context)
}

public void callMyClass(Context context)
{
    MyClass myClass= new myClass(context);
    myClass.generateTrigger(context);
}

EDIT

The problem was BECAUSE of accessing different instances of MyClass in BroadcastReceiver and Listeners list as correctly pointed out by @Binyamin Sharet

The solution is to declare the listener list as static and also public void generateTrigger() as static so that the receiver’s callMyClass looks like :

public void callMyClass(Context context)
{
    MyClass.generateTrigger(context);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:26:31+00:00Added an answer on June 1, 2026 at 8:26 am

    You never instantiate listener (declared as CustomListener listener;) and therefore its always null, you just need to set the activity as the listener, as it implements the interface:

    myClass.setOnCustomListener(this);
    

    As seen in your code, you create a new instance of the class in the receiver, so the listener you set does not exist in the listeners list of new instance, since the list is not static.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have on Service that must communicate with an Activity (MyActivity) through a BradcastReceiver.
I have an activity that implements LocationListener in my application and my onLocationChanged method
I have a class that implements getter and setter methods and related code as
I have a very simple code where I use Action Bar with tab fragments.
Here's my code: public class MyActivity extends Activity implements View.OnClickListener { private Button mHorizontalButton;
I have this code: public class VoiceActivity extends Activity implements OnClickListener { private TextView
I'm learning android but have a pretty fair background in java so am a
My activity implements OnTouchListener and it have one ListView inside it. When user touch
I've my main custom listactivity class that have a instance object of my custom
My activity implements KeyListener and my edittext has a key listener set. editor =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.