2016-03-16 76 views
-1

我想創建一個使用導航抽屜的菜單活動。起初我用getActionBar(),然後我發現,我本來是要改變getSupportActionBar()和擴展AppCompactActivity其中II沒有,但我似乎仍然得到同樣的錯誤:Android錯誤[嘗試調用虛擬方法'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)]

java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' 
on a null object reference 

任何人都可以知道可能導致問題的原因。 (請讓我知道如果我要上傳任何代碼)

這裏是我的相應代碼:

mTitle = mDrawerTitle = getTitle(); 

    // load slide menu items 
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

    // nav drawer icons from resources 
    navMenuIcons = getResources() 
      .obtainTypedArray(R.array.nav_drawer_icons); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu); 

    navDrawerItems = new ArrayList<NavDrawerItem>(); 

    // adding nav drawer items to array 
    // Home 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1))); 
    // Find People 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1))); 
    // Photos 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); 
    // Communities, Will add a counter here 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22")); 
    // Pages 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1))); 
    // What's hot, We will add a counter here 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+")); 


    // Recycle the typed array 
    navMenuIcons.recycle(); 

    // setting the nav drawer list adapter 
    adapter = new NavDrawerListAdapter(getApplicationContext(), 
      navDrawerItems); 
    mDrawerList.setAdapter(adapter); 

    // enabling action bar app icon and behaving it as toggle button 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.ic_drawer, //nav menu toggle icon 
      R.string.app_name, // nav drawer open - description for accessibility 
      R.string.app_name // nav drawer close - description for accessibility 
    ){ 
     public void onDrawerClosed(View view) { 
      getSupportActionBar().setTitle(
        Html.fromHtml("<font color=\"black\">" + mTitle + "</font>")); 

      // calling onPrepareOptionsMenu() to show action bar icons 
      invalidateOptionsMenu(); 
     } 

     public void onDrawerOpened(View drawerView) { 
      getSupportActionBar().setTitle(
        Html.fromHtml("<font color=\"black\">" + mDrawerTitle + "</font>")); 

      // calling onPrepareOptionsMenu() to hide action bar icons 
      invalidateOptionsMenu(); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    if (savedInstanceState == null) { 
     // on first time display view for first nav item 
     //displayView(0); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_menu, menu); 
    return true; 
} 
+0

張貼你的java代碼 –

+0

你使用ActionBar嗎?或工具欄? –

+3

可能的重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) – Selvin

回答

0

在你onCreate方法執行以下操作

public class ActivityDemo extends AppCompatActivity{ 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_demo); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     //other statements 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       this.finish(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
    } 

相應的XML應該具有類似於內容這

<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.dev.ActivityDemo"> 

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

     <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_settings" 
     /> 
相關問題