1
votes

I've tried to use an OnClickListener from a different class but somehow it throws me an error. Can someone help me to solve this problem?

Thanks in advance.

public class TestClass extends Activity{



    View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Clicked", Toast.LENGTH_LONG).show();
        }};}

Part of the MainActivity:

@Override protected void onCreate(Bundle savedInstanceState) {
           ...
           btnSpeech = (ImageButton) (findViewById(R.id.microphone));


           obj=new TestClass();
           btnSpeech.setOnClickListener(obj.l);

           ...

Error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.project/com.example.user.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.user.project.MainActivity.onCreate(MainActivity.java:74) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method) 

2
If that stack trace is for that code, then btnSpeech is null. Also, TestClass and its listener aren't going to work as you expect. You can't instantiate an Activity with new and have it work correctly. You can, instead, remove extends Activity, and replace getApplicationContext() with v.getContext(). Though, I don't see why you just don't create the listener in MainActivity.Mike M.
Thanks, but it didn't work it gave me again the NullPointerException. I know you could do this easily within the MainActivity but I only wanted to try whether/ how you could start an OnClickListener from a seperate class and I've followed many suggestions from the internet but somehow it didn't work for me.AndrIOS
If the current stack trace is the same as you posted above, then btnSpeech is still null. You've not provided us with enough information to determine why, though.Mike M.
Thanks for response but what kind of information do you need, I thought based on the stack trace the main source of the problem has to be the part with btnSpeech and the OnClickListener so I decided to pick only this part because my MainActitvity is quite big to post it as a whole.AndrIOS
Well, make sure that the Button with the ID microphone is in the layout that you're using in the setContentView() call in onCreate(), and that you're calling setContentView() before initializing btnSpeech with findViewById(). If neither of those are the problem, then we'll need to see more code, like the whole onCreate() method, and the layout, including its filename.Mike M.

2 Answers

1
votes

You get a NullPointerException because your btnSpeech is null. findViewById() returns null if you use a wrong id for the view, maybe this is the problem. What is sure is that your exception has nothing to do with the OnClickListener. If you read your stacktrace carefully you see that it says that setOnClickListener() was called on a object that was null.

And, as Mike commented, you cannot instantiate activities with the new keyword. Use startActivity() with an intent or make TestClass not extend Activity.

1
votes

this is what you have just done quickly, but not if this will solve your problem

public class TestClass {

    public static Context context;

    public static View.OnClickListener getListener(){
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
            }
        };
    }

}

In Activity

 TestClass.context = this;
 my_button.setOnClickListener(TestClass.getListener());

Hope this help..