ActionBarDrawerToggle中的NullPointerException,导航抽屉,android?

时间:2015-03-13 09:58:55

标签: android nullpointerexception navigation-drawer actionbardrawertoggle

我在ActionBarDrawerToggle

中收到NullPointerException

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);
}

1 个答案:

答案 0 :(得分:2)

我建议删除onPostCreate()并使用以下代码同步切换,google示例应用使用此

mDrawerLayout.post(new Runnable() {

            @Override
            public void run() {

                mDrawerToggle.syncState();

            }

        });