我正在尝试在我的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开发人员的新手,非常感谢任何帮助。 另外,如果你告诉我为什么会出现这个错误,那将会很棒,所以我将来可以避免它。
谢谢
答案 0 :(得分:0)
从支持操作栏更改为ActionBar应解决此问题:
getActionBar().setDisplayHomeAsUpEnabled(true);