2016-07-05 75 views
-1

我有一個AppCompatActivity的標準設置。 要啓用向上按鈕activity我需要致電getSupportActionBar().setDisplayHomeAsUpEnabled(true);NullPointerException:空對象上的setDisplayHomeAsUpEnabled(boolean)參考

有很多相似threads。不幸的是,我無法找到我的錯誤,因爲我使用AppCompatActivity代替ActivitygetSupportActionBar()代替getActionBar()

活動:

public class GalleryActivity extends AppCompatActivity { 

    (...) 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gallery_view); 

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

     (...) 
    } 
} 

activity_gallery_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<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_gallery" 
      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/gallery_content" /> 

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

styles.xml

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

</resources> 

堆棧跟蹤:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stack.overflow/com.stack.overflow.reviewmaschine.gallery.GalleryActivity}: 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:2416) 
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                    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 android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference 
                    at 

com.stack.overflow.reviewmaschine.gallery.GalleryActivity.onCreate(GalleryActivity.java:56) 
                    at android.app.Activity.performCreate(Activity.java:6251) 
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                    at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                    at android.os.Handler.dispatchMessage(Handler.java:102)  
                    at android.os.Looper.loop(Looper.java:148)  
                    at android.app.ActivityThread.main(ActivityThread.java:5417)  
                    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) 

+0

做你檢查你的工具欄的ID?將findViewById()用於xml時,您已經給出了不同的id –

回答

2

你已經給xml和java文件中的工具欄提供了不同的id。讓它們相同,最有可能你的錯誤應該消失。 這是你的錯誤

android:id="@+id/toolbar_gallery" 

而且在活動的java文件,你已經做了

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

解決這個問題

2

設置它,你得到它之前。

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

變化setToolbarActionbar和getSupportActionbar像這樣的順序:

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

你簡單地嘗試將其設置編程之前沒有收到您supportActionbar。這就是爲什麼有一個空指針異常

相關問題