Navigation Drawer项目运行报错:空指针异常问题求助
我是一名学生,在开发项目中尝试运行Navigation Drawer时出现错误。因英文水平有限,借助翻译工具表述问题,望各位理解。
报错信息
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ce160343.exe_4/com.ce160343.exe_4.TestActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:234) at android.app.ActivityThread.main(ActivityThread.java:5526) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at com.ce160343.exe_4.TestActivity.onCreate(TestActivity.java:28) at android.app.Activity.performCreate(Activity.java:6285) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:234) at android.app.ActivityThread.main(ActivityThread.java:5526) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
相关代码
TestActivity.java
package com.ce160343.exe_4; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.drawerlayout.widget.DrawerLayout; import android.os.Bundle; import android.view.MenuItem; public class TestActivity extends AppCompatActivity { public DrawerLayout drawerLayout; public ActionBarDrawerToggle actionBarDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); drawerLayout = findViewById(R.id.my_drawer_layout); actionBarDrawerToggle = new ActionBarDrawerToggle(TestActivity.this, drawerLayout, R.string.nav_open, R.string.nav_close); drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } }
activity_test.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/my_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".TestActivity" tools:ignore="HardcodedText"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="128dp" android:gravity="center" android:text="Name" android:textSize="18sp" /> </LinearLayout> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/navigation_menu" /> </androidx.drawerlayout.widget.DrawerLayout>
navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" tools:ignore="HardcodedText"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_kp" android:title="@string/nav_kp" android:icon="@drawable/ic_kp" /> <item android:id="@+id/nav_dvm" android:title="@string/nav_dvm" android:icon="@drawable/ic_dvm" /> <item android:id="@+id/nav_gmnb" android:title="@string/nav_gmnb" android:icon="@drawable/ic_gmnb"/> <item android:id="@+id/nav_dsyt" android:title="@string/nav_dsyt" android:icon="@drawable/ic_dsyt"/> <item android:id="@+id/nav_dv" android:title="@string/nav_dv" android:icon="@drawable/ic_dv"/> <item android:id="@+id/nav_eykh" android:title="@string/nav_eykh" android:icon="@drawable/ic_eykh"/> <item android:id="@+id/nav_cstc" android:title="@string/nav_cstc" android:icon="@drawable/ic_cstc"/> <item android:id="@+id/nav_hd" android:title="@string/nav_hd" android:icon="@drawable/ic_hd"/> <item android:id="@+id/nav_tg" android:title="@string/nav_tg" android:icon="@drawable/ic_tg"/> </group> </menu>
问题分析与解决办法
报错核心是getSupportActionBar()返回null,调用setDisplayHomeAsUpEnabled(true)时触发空指针异常,原因是当前Activity使用的主题没有自带ActionBar。
解决方式二选一:
方式一:更换带ActionBar的主题
- 打开
res/values/styles.xml,修改Activity的主题为带ActionBar的样式,比如:<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- 其他自定义样式 --> </style> - 确保
AndroidManifest.xml中TestActivity引用该主题:<activity android:name=".TestActivity" android:theme="@style/AppTheme"/>
方式二:手动添加Toolbar
如果需要使用无ActionBar的主题,可通过Toolbar替代:
- 修改
activity_test.xml,在主LinearLayout中添加Toolbar:<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="128dp" android:gravity="center" android:text="Name" android:textSize="18sp" /> </LinearLayout> - 修改TestActivity代码,初始化Toolbar并设置为ActionBar:
package com.ce160343.exe_4; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.drawerlayout.widget.DrawerLayout; import android.os.Bundle; import android.view.MenuItem; public class TestActivity extends AppCompatActivity { public DrawerLayout drawerLayout; public ActionBarDrawerToggle actionBarDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); // 初始化Toolbar Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawerLayout = findViewById(R.id.my_drawer_layout); // 传入Toolbar构造ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(TestActivity.this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close); drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } }
完成以上操作后,重新运行项目即可解决空指针问题,Navigation Drawer就能正常工作。
内容的提问来源于stack exchange,提问作者Nathan Diep
相关产品推荐
相关产品推荐

