如何在我的Android应用程序中使用Android导航栏时解决Java空指针异常
我收到一个运行时错误: 致命异常:主如何在我的Android应用程序中使用Android导航栏时解决Java空指针异常,android,android-layout,Android,Android Layout,我收到一个运行时错误: 致命异常:主 进程:com.example.home.stobun,PID:18938 java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.home.stobun/com.example.home.stobun.drawer}:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.app.ActionBar.setDisplayH
进程:com.example.home.stobun,PID:18938
java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.home.stobun/com.example.home.stobun.drawer}:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)”
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)上 当我尝试实现以下代码时: 下面是我的java类:
}
下面是相应的XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Widget.Design.AppBarLayout"
android:id="@+id/drawer"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="@menu/nav_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
应用程序在运行时崩溃。您正在调用getActionBar()
调用getSupportActionBar()
您需要使用getSupportActionBar()
代替getActionBar()
像这样-
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
你好,阿披舍克,请检查我下面的答案可能是
getSupportActionBar().setDisplayHomeAsUpEnabled(true);