2017-04-06 43 views
0

首先,我知道這個問題之前已經被問到過,我花了一個小時看其他解決方案,主要是從 - getActionBar() returns null,沒有一個工作,所以另一雙眼睛可能能夠做些事情。getSupportActionBar()。setDisplayHomeAsUpEnabled(true);拋出NullPointerException

NavigationDrawer類其他類繼承:

public class NavigationDrawer extends AppCompatActivity { 

String[] mDrawerTitles; 
DrawerLayout mDrawerLayout; 
ListView mDrawerList; 
ActionBarDrawerToggle mDrawerToggle; 
String activityTitle; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.navigation_drawer_layout); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    mDrawerTitles = getResources().getStringArray(R.array.drawer_array); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.left_drawer); 
    activityTitle = getTitle().toString(); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //here is where the error is thrown 
    getSupportActionBar().setHomeButtonEnabled(true); 

    addDrawerItems(); 
    navDrawerSetup(); 

} 

MainActiviy延伸以上:

public class MainActivity extends NavigationDrawer { 

int[]images={R.drawable.user_manual,R.drawable.tips_tricks, R.drawable.troubleshooting}; 
Integer printer; 
ListView myListView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View contentView = inflater.inflate(R.layout.activity_main, null, false); 
    mDrawerLayout.addView(contentView, 0); 
} 

抽屜式導航的.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.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/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ListView 
    android:id="@+id/left_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#999" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" /> 


</android.support.v4.widget.DrawerLayout> 

styles.xml

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- 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.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

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

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

</resources> 

activity_main

<?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" 
tools:context="com.example.shelleyd.myapplication.MainActivity"> 

<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> 

content_main

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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/content_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.shelleyd.myapplication.MainActivity" 
tools:showIn="@layout/activity_main"> 

<ListView 
    android:id="@+id/myList" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" /> 

</LinearLayout> 

回答

1

NavigationDrawer活動要執行setContentView(R.layout.navigation_drawer_layout),並在navigation_drawer_layout沒有任何Toolbar,從而導致異常。

+0

是的,這是它的感謝。該應用程序現在打開,但沒有導航抽屜顯示,任何想法爲什麼? – Norno3

+0

你嚴重濫用繼承權。分開這些活動。它會讓你的代碼更清潔,讓你的生活更輕鬆。 – azizbekian

+0

好的,我只是按照這個 - http://stackoverflow.com/a/25254794,並認爲我也是這樣做的。我想現在我會在每個活動中恢復爲導航代碼。 – Norno3

1

做你NavigationDrawer.class

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //This is null 
setSupportActionBar(toolbar); 

下面要設置活動工具欄爲空,因爲在你的R.layout.activity_main與R.id.toolbar ID沒有意見。因此,當您嘗試在上面的代碼之後設置工具欄時,您試圖在空對象中設置一個屬性,從而產生空指針異常。

在您的活動中刪除上面兩行或使用R.layout.activity_main佈局文件中的R.id.toolbar ID聲明工具欄視圖。這樣做會解決問題。

相關問題