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)
btnSpeech
is null. Also,TestClass
and its listener aren't going to work as you expect. You can't instantiate anActivity
withnew
and have it work correctly. You can, instead, removeextends Activity
, and replacegetApplicationContext()
withv.getContext()
. Though, I don't see why you just don't create the listener inMainActivity
. – Mike M.btnSpeech
is still null. You've not provided us with enough information to determine why, though. – Mike M.Button
with the IDmicrophone
is in the layout that you're using in thesetContentView()
call inonCreate()
, and that you're callingsetContentView()
before initializingbtnSpeech
withfindViewById()
. If neither of those are the problem, then we'll need to see more code, like the wholeonCreate()
method, and the layout, including its filename. – Mike M.