2013-10-13 161 views
1

不知怎的,我在此得到一個空指針異常在這種方法錯誤抽屜式導航

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    actionBarToggle.syncState(); 
} 

actionBarToggle.syncState()有一個空指針異常。

現在,如果我註釋掉這個再有就是空指針異常,當我觸摸動作條,打開導航抽屜

if (actionBarToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 

回答

2

我發現這個問題實際上我在做這個

 actionBarToggle = new ActionBarDrawerToggle(this, drawerLayout, 
      R.drawable.ic_drawer, R.string.drawerOpen, R.string.drawerClose) { 
     public void onDrawerClosed(View view) { 
      getSupportActionBar().setTitle("Close"); 
      ActivityCompat.invalidateOptionsMenu(activity); 
     } 

     public void onDrawerOpened(View main) { 
      getSupportActionBar().setTitle("Open"); 
      ActivityCompat.invalidateOptionsMenu(activity); 
     } 
    }; 
    drawerLayout.setDrawerListener(actionBarToggle); 
    drawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);//This should be before actionBarToggle 

於是我就在actionBarToggle使用後intailizing的drawerLayout。

0

我也有NullPointerException。這是一個愚蠢的錯誤,我在做:

drawerToggle = new ActionBarDrawerToggle(this, 
      drawerLayout, 
      R.drawable.ic_drawer, 
      1, 
      2){ ... } 

我硬編碼無障礙環境這兩個字符串說1,2,因爲我發現這些字符串不重要。據我運行Android 2.3-4.3一切都很好,但在4.4我得到一個異常。 解決方案很簡單:只需在values/strings.xml中創建這兩個字符串

對不起,我的英語:)