This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                                2年前关闭。
            
                    
我正在尝试在我的Android应用中实现一个简单的导航抽屉,但无法解决某些错误。

尝试运行应用程序将引发NullPointerException:

Process: com.emertonalex.whiskynotes04, PID: 22225
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emertonalex.whiskynotes04/com.emertonalex.whiskynotes04.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                  at com.emertonalex.whiskynotes04.MainActivity.setupToolbar(MainActivity.java:320)
                  at com.emertonalex.whiskynotes04.MainActivity.onCreate(MainActivity.java:97)
                  at android.app.Activity.performCreate(Activity.java:6999)
                  at android.app.Activity.performCreate(Activity.java:6990)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                  at android.os.Handler.dispatchMessage(Handler.java:106) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6494) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 


据我所知,getSupportActionBar()函数调用使事情变得混乱。我尝试在setupToolbar()中使用“断言”覆盖该呼叫,看来它已通过该阶段并在

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

调用getSupportActionBar()的代码本身如下:

在MainActivity中

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.add_notes);
   ...
   setupToolbar();

   getSupportActionBar().setDisplayHomeAsUpEnabled(false);
   getSupportActionBar().setHomeButtonEnabled(true);
   ...
}




void setupToolbar(){
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}


我是Android / Java开发人员的新手,将非常感谢您的帮助。
同样,如果您告诉我为什么会出现此错误,那将是很好的,因此我将来可以避免它。

谢谢

最佳答案

从支持操作栏更改为操作栏应解决此问题:

getActionBar().setDisplayHomeAsUpEnabled(true);

10-06 14:06