androiddrawerlayoutactionbardrawertoggle

Exception at mydrawer.setDrawerListener(myactiontoggle) in Android


I am using drawer in my project but sometimes it throw NullpointerException, below is my code where I got exception:

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

And the below error i got:

    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) 

 

please help to solve his NullpointerException in ActionBarDrawerToggle.

This question not just about NullpointerException exception as tell me duplicate like: NullpointerException


Solution

  • in above code you have pass toolbar as a null thats why its gives you java.lang.NullPointerException

    Here is Sample code

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