0

我在ActionBarDrawerToggle得到NullPointerException异常ActionBarDrawerToggle中的NullPointerException,导航抽屉,android?

的logcat:

Caused by: java.lang.NullPointerException 

at com.appp.AppActivity.onPostCreate(AppActivity.java:482) 
at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1150) 

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229) 

获取错误onPostCreate

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

创建通过Java代码布局

void addSideMenu() { 
    mDrawerLayout = new DrawerLayout(this); 
    mDrawerLayout.setLayoutParams(new DrawerLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    FrameLayout content_frame = new FrameLayout(this); 
    content_frame.setLayoutParams(new DrawerLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

    content_layout = new LinearLayout(this); 
    content_layout.setOrientation(LinearLayout.VERTICAL); 
    content_layout.setLayoutParams(new LinearLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

    Toolbar tool = new Toolbar(this); 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

    tool.setLayoutParams(lp); 
    tool.setBackgroundColor(Color.parseColor("#00BBD3")); 
    setSupportActionBar(tool); 
    TypedValue tv = new TypedValue(); 
    if (this.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, 
      true)) { 
     int actionBarHeight = TypedValue.complexToDimensionPixelSize(
       tv.data, this.getResources().getDisplayMetrics()); 
     tool.setMinimumHeight(actionBarHeight); 
    } 

    ListView mDrawerList = new ListView(this); 
    mDrawerList.setLayoutParams(new DrawerLayout.LayoutParams(320, 
      LayoutParams.MATCH_PARENT, Gravity.START)); 
    mDrawerList.setBackgroundColor(Color.BLACK); 
    content_layout.addView(tool); 
    addWebView(); 

    mDrawerLayout.addView(mDrawerList); 
    mDrawerLayout.addView(content_layout); 
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, tool, 
      R.string.action_settings, R.string.action_settings) { 

     /** Called when a drawer has settled in a completely closed state. */ 
     public void onDrawerClosed(View view) { 
      super.onDrawerClosed(view); 
      invalidateOptionsMenu(); // creates call to 
             // onPrepareOptionsMenu() 
     } 

     /** Called when a drawer has settled in a completely open state. */ 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
      invalidateOptionsMenu(); // creates call to 
             // onPrepareOptionsMenu() 
     } 
    }; 
    setContentView(mDrawerLayout); 

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
      R.layout.email_element, 
      listArray)); 


    // Set the drawer toggle as the DrawerListener 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 
} 
+0

'mDrawerToggle == NULL检查it.' – 2015-03-13 09:59:45

+0

它不是空已经宣布 – WISHY 2015-03-13 10:01:31

+0

你可以发布更多的代码?或者至少''onCreate'方法初始化'mDrawerToggle'变量? – 2015-03-13 10:02:59

回答

2

我会建议删除onPostCreate()一个D使用下面的代码同步切换,谷歌示例应用程序使用此

mDrawerLayout.post(new Runnable() { 

      @Override 
      public void run() { 

       mDrawerToggle.syncState(); 

      } 

     });