单独的类中的OnClickListener将不起作用

时间:2016-04-12 23:26:44

标签: java android

我尝试使用来自不同类的OnClickListener,但不知怎的,它会抛出一个错误。有人可以帮我解决这个问题吗?

提前致谢。

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();
        }};}

MainActivity的一部分:

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


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

           ...

错误:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.user.project / com.example.user.project.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget null对象引用上的.ImageButton.setOnClickListener(android.view.View $ OnClickListener)'                                                                                     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)                                                                                     在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                     在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                     在android.os.Looper.loop(Looper.java:148)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                                                                                  引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ImageButton.setOnClickListener(android.view.View $ OnClickListener)'                                                                                     在com.example.user.project.MainActivity.onCreate(MainActivity.java:74)                                                                                     在android.app.Activity.performCreate(Activity.java:6237)                                                                                     在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)                                                                                     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)                                                                                     在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                     在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                     在android.os.Looper.loop(Looper.java:148)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                     at java.lang.reflect.Method.invoke(Native Method)

2 个答案:

答案 0 :(得分:1)

您得到NullPointerException,因为btnSpeech为空。如果您对视图使用了错误的id,findViewById()将返回null,这可能是问题所在。 可以肯定的是,您的异常与OnClickListener无关。如果您仔细阅读了堆栈跟踪,则会看到它表示在一个空的对象上调用了setOnClickListener()

而且,正如Mike评论的那样,您无法使用new关键字实例化活动。有意使用startActivity()TestClass不延长Activity

答案 1 :(得分:1)

这是你刚刚完成的事情,但如果这样可以解决你的问题,那就不行了

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();
            }
        };
    }

}

活动

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

希望这有帮助..