如何使用此方法a getActionBar()。setDisplayHomeAsUpEnabled(true);

时间:2017-06-27 06:40:30

标签: android

我收到错误:

time: Shutting down VM
06-27 11:52:11.363 31628-31628/com.example.android.bleiqn E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.bleiqn, PID: 31628
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bleiqn/com.example.android.bleiqn.PeripheralActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
        at android.app.ActivityThread.access$900(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5441)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
        at com.example.android.bleiqn.PeripheralActivity.onCreate(PeripheralActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:6303)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
        at android.app.ActivityThread.access$900(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5441) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 

logcat的:

root
|___ app
      |___ a.rb
      |___ b.rb
      |___ z.rb
|___ spec
      |___ app
            |___ a_spec.rb
            |___ b_spec.rb
            |___ z_spec.rb
      |___ tasks
            |___ atask_spec.rb
      |___ spec_helper.rb
|____ tasks
      |___ atask.rake

我该如何解决?

2 个答案:

答案 0 :(得分:0)

很难从您的活动类中确定错误,它可能来自您的主题,布局或操作栏类型。最好也发布xml文件。

看起来你要调用操作栏而不向布局添加操作栏,这就是为什么它给出了空对象引用。

如果您已将操作栏添加为工具栏,请从工具栏创建对象并将其设置为操作栏作为setSupportActionBar(工具栏);

希望这会有所帮助。

答案 1 :(得分:0)

在xml文件中添加ToolBar:

<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_capture_and_upload_loan_paper" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

并在活动文件中使用ToolBar id:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);      
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);