找到你要的答案

Q:onOptionsItemSelected unexpectedly closes drawer

Q:onOptionsItemSelected意外关闭抽屉

I am creating a navigation drawer following this guide.

I did everything but the app forces close when pressing the icon that opens the drawer.

When I remove this:

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

located under the onOptionsItemSelected() method the app doesn't force close but the icon doesn't open the nav drawer.

logcat

05-02 08:23:30.560    2449-2449/com.mrad4tech.drawwer.mrad4techdr D/AndroidRuntime﹕ Shutting down VM 05-02 08:23:30.561    2449-2449/com.mrad4tech.drawwer.mrad4techdr E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mrad4tech.drawwer.mrad4techdr, PID: 2449
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.app.ActionBarDrawerToggle.onOptionsItemSelected(android.view.MenuItem)' on a null object reference
            at com.mrad4tech.drawwer.mrad4techdr.webview.onOptionsItemSelected(webview.java:98)
            at android.app.Activity.onMenuItemSelected(Activity.java:2882)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
            at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
            at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
            at android.support.v7.internal.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:190)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 05-02 08:23:30.567    1232-2242/system_process W/ActivityManager﹕ Force finishing activity com.mrad4tech.drawwer.mrad4techdr/.webview 05-02 08:23:30.579    1232-1280/system_process W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client 05-02 08:23:30.581    2449-2464/com.mrad4tech.drawwer.mrad4techdr E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44 05-02 08:23:30.622    2449-2464/com.mrad4tech.drawwer.mrad4techdr E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0 05-02 08:23:30.689    1232-2199/system_process I/OpenGLRenderer﹕ Initialized EGL, version 1.4 
05-02 08:23:30.746    1232-2199/system_process W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
05-02 08:23:30.746    1232-2199/system_process W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb0a14820, error=EGL_SUCCESS 
05-02 08:23:30.791    2449-2464/com.mrad4tech.drawwer.mrad4techdr E/eglCodecCommon﹕ **** ERROR unknown type 0x73000d (glSizeof,72) 
05-02 08:23:31.777    1232-1250/system_process I/Choreographer﹕ Skipped 56 frames!  The application may be doing too much work on its main thread. 
05-02 08:23:31.814    1232-1250/system_process W/ActivityManager﹕ Activity pause timeout for ActivityRecord{1c9d519c u0 com.mrad4tech.drawwer.mrad4techdr/.webview t70 f} 
05-02 08:23:40.573    1232-1250/system_process W/ActivityManager﹕ Launch timeout has expired, giving up wake lock!

我创建一个导航抽屉如下指南。

我做的一切,但应用程序部队关闭时按下图标打开抽屉。

当我移除这个:

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

位于onoptionsitemselected()方法在应用程序不强制关闭图标却不打开导航抽屉。

logcat

05-02 08:23:30.560    2449-2449/com.mrad4tech.drawwer.mrad4techdr D/AndroidRuntime﹕ Shutting down VM 05-02 08:23:30.561    2449-2449/com.mrad4tech.drawwer.mrad4techdr E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mrad4tech.drawwer.mrad4techdr, PID: 2449
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.app.ActionBarDrawerToggle.onOptionsItemSelected(android.view.MenuItem)' on a null object reference
            at com.mrad4tech.drawwer.mrad4techdr.webview.onOptionsItemSelected(webview.java:98)
            at android.app.Activity.onMenuItemSelected(Activity.java:2882)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
            at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
            at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
            at android.support.v7.internal.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:190)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 05-02 08:23:30.567    1232-2242/system_process W/ActivityManager﹕ Force finishing activity com.mrad4tech.drawwer.mrad4techdr/.webview 05-02 08:23:30.579    1232-1280/system_process W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client 05-02 08:23:30.581    2449-2464/com.mrad4tech.drawwer.mrad4techdr E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44 05-02 08:23:30.622    2449-2464/com.mrad4tech.drawwer.mrad4techdr E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0 05-02 08:23:30.689    1232-2199/system_process I/OpenGLRenderer﹕ Initialized EGL, version 1.4 
05-02 08:23:30.746    1232-2199/system_process W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
05-02 08:23:30.746    1232-2199/system_process W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb0a14820, error=EGL_SUCCESS 
05-02 08:23:30.791    2449-2464/com.mrad4tech.drawwer.mrad4techdr E/eglCodecCommon﹕ **** ERROR unknown type 0x73000d (glSizeof,72) 
05-02 08:23:31.777    1232-1250/system_process I/Choreographer﹕ Skipped 56 frames!  The application may be doing too much work on its main thread. 
05-02 08:23:31.814    1232-1250/system_process W/ActivityManager﹕ Activity pause timeout for ActivityRecord{1c9d519c u0 com.mrad4tech.drawwer.mrad4techdr/.webview t70 f} 
05-02 08:23:40.573    1232-1250/system_process W/ActivityManager﹕ Launch timeout has expired, giving up wake lock!
answer1: 回答1:

Try returning false

if (mDrawerToggle.onOptionsItemSelected(item)) {
return false;
}

尝试返回false

if (mDrawerToggle.onOptionsItemSelected(item)) {
return false;
}
answer2: 回答2:

mDrawerToggle is null in your activity, maybe not initialised before

mdrawertoggle在你的活动是无效的,也许没有初始化之前

answer3: 回答3:

May your mDrawerToggle is not initialised or not initialized correctly. Try this code instead:

drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); //This is your DrawerLayout
//I guess you are using the toolbar widget
        mDrawerToggle= new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        drawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

Hope this will helpful.

May your mDrawerToggle is not initialised or not initialized correctly. Try this code instead:

drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); //This is your DrawerLayout
//I guess you are using the toolbar widget
        mDrawerToggle= new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        drawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

希望这会有帮助。

android  android-studio