实现View.OnclickListener,将setOnClickListener引用到' this'变量......导致NullPointerException并且无法启动Activity

时间:2015-05-01 07:43:43

标签: android android-activity

错误日志如下所示

05-01 12:33:41.993    3145-3145/com.example.barun.myapplication D/AndroidRuntime﹕ Shutting down VM
05-01 12:33:41.993    3145-3145/com.example.barun.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.barun.myapplication, PID: 3145
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.barun.myapplication/com.example.barun.myapplication.TextPlay}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.barun.myapplication.TextPlay.onCreate(TextPlay.java:32)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我是Android的初学者,我尝试了一种不同的方式(通过实现View.OnClickListener并引用类本身)来调用setOnClickListener(),如下面的 TextPlay 类,它失败了。

方法1:

public class TextPlay extends Activity implements View.OnClickListener {
    Button chkCmd;
    ToggleButton passTog;
    EditText input;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);

        baconAndEggs();
        chkCmd.setOnClickListener(this);
        passTog.setOnClickListener(this);

    }

    private void baconAndEggs() {
         Button chkCmd = (Button) findViewById(R.id.bResult);
         ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
         EditText input = (EditText) findViewById(R.id.etCommands);
         TextView display = (TextView) findViewById(R.id.tvResults);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bResult:
                break;
            case R.id.tbPassword:

                break;
        }
    }
}

但是如果我没有实现View.OnClickListener并且创建View.OnClickListener的新实例作为setOnClickListener()方法的参数,它确实可以正常工作。

方法2:

    chkCmd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //required code here
        }
    });

为什么会这样?我已经关注了this Youtube link以及一些博客,其中包括方法1和方法1。 2根据他们的建议很好,但对我来说只有方法2正在工作,在方法1上它是运行时错误。

1 个答案:

答案 0 :(得分:1)

gtest.lib内,您为每个对象创建一个局部变量,而不是分配类变量。因此,类变量保持为空。

从此改变:

baconAndEggs

到此:

private void baconAndEggs() {
     Button chkCmd = (Button) findViewById(R.id.bResult);
     ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
     EditText input = (EditText) findViewById(R.id.etCommands);
     TextView display = (TextView) findViewById(R.id.tvResults);
}