-1

我在我的项目中使用抽屉,但有时它会抛出 NullpointerException,下面是我遇到异常的代码:

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new ActionBarDrawerToggle(TabSample.this, drawer, null, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);//here i got exception
    toggle.syncState();

我得到了以下错误:

    01-23 11:53:20.370 16530-16530/com.myproject.myproject1 E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.myproject1/com.myproject.myproject1.TabSample}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
    at android.app.ActivityThread.access$600(ActivityThread.java:128)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4517)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at android.support.v7.app.ActionBarDrawerToggle$HoneycombDelegate.setActionBarUpIndicator(ActionBarDrawerToggle.java:539)
    at android.support.v7.app.ActionBarDrawerToggle.setActionBarUpIndicator(ActionBarDrawerToggle.java:458)
    at android.support.v7.app.ActionBarDrawerToggle.syncState(ActionBarDrawerToggle.java:245)
    at com.myproject.myproject1.TabSample.setDrawer(TabSample.java:109)
    at com.myproject.myproject1.TabSample.onCreate(TabSample.java:77)
    at android.app.Activity.performCreate(Activity.java:4470)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 
    at android.app.ActivityThread.access$600(ActivityThread.java:128) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4517) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 
    at dalvik.system.NativeStart.main(Native Method) 

 

请帮助解决他在 ActionBarDrawerToggle 中的 NullpointerException。

这个问题不仅仅是关于 NullpointerException 异常,因为告诉我重复,如: NullpointerException

4

1 回答 1

1

在上面的代码中,您将工具栏作为 null 传递,这就是它给您 java.lang.NullPointerException 的原因

这是示例代码

toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
于 2017-01-23T06:46:48.883 回答