为什么BUTTON.setEnabled(true);如果BUTTON不为空,会导致nullpointer异常?

时间:2014-12-13 23:22:15

标签: android

我得到了

 Caused by: java.lang.NullPointerException
        at ente.testen.Testen.onCreate(Testen.java:210)

指向

            btnAendern.setEnabled(true);

    final Button btnAendern = (Button) findViewById(R.id.btnAendern);

    final Intent absicht = getIntent();
        if (absicht == null)
            btnAendern.setEnabled(false);
        else
            btnAendern.setEnabled(true);

Android Studio 1.0没有显示R.id.btnAendern下划线,因此确实找到了它。使用

    if (btnAendern != null) {
        if (absicht == null)
            btnAendern.setEnabled(false);
        else
            btnAendern.setEnabled(true);
    }

仍然让Android指向btnAendern.setEnabled(true);

原因可能是什么?

以下是完整的错误日志:

12-14 00:23:27.949  17965-17965/ente.testen E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ente.testen, PID: 17965
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ente.testen/ente.testen.testen}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5097)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at ente.testen.testen.onCreate(testen.java:210)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5097)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

IDE发现它并不意味着该按钮存在。如果您尚未设置内容视图,或者您设置的内容视图没有该按钮,则仍会返回null。其中之一就是这种情况。