我正在尝试在android中创建侧边栏导航,但是我面临的一个问题。我在stackoverflow上经历了很多帖子,但并没有解决我的问题
我正在遵循完全提到的here
教程但是当我运行我的应用程序时,我得到了错误
Process: com.example.deepak.myapplication, PID: 26704
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deepak.myapplication/com.example.deepak.myapplication.Naviagtion}: 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:2721)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6228)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
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.example.deepak.myapplication.Naviagtion.onCreate(Naviagtion.java:29)
at android.app.Activity.performCreate(Activity.java:6871)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2674)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6228)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
我正在使用提到的样式
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
我也在扩展AppCompatActivity
所以请帮助我,让我知道我要去哪里了
答案 0 :(得分:1)
您尚未设置ActionBar,请将此行放在
上方 setSupportActionBar(toolbar); // <--- put this
ActionBar.setDisplayHomeAsUpEnabled(true)