androidandroid-navigationactionbardrawertoggle

nullpointerexception on calling syncstate() on ActionBarDrawerToggle object


I am getting Null pointer exception on calling the syncstate() function on ActionBarDrawerToggle object.

Navigation_logined.java

public class Navigation_logined extends Fragment {
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
public Navigation_logined(){

}
public void setUp(DrawerLayout drawerLayout,Toolbar toolbar)
{
    mDrawerLayout=drawerLayout;
    mDrawerToggle=new ActionBarDrawerToggle(getActivity(),mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);

        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

        }
    };

    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
           //nullpointerexception at this point
           mDrawerToggle.syncState();
        }
    });
    mDrawerLayout.setDrawerListener(mDrawerToggle);


}
//rest of the class
}

logined.java

public class logined extends ActionBarActivity {
protected Toolbar mToolBar;
String LOGIN_URL="";
static public Context mContext;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResourceIdentifier());

    mToolBar=(Toolbar)findViewById(R.id.screen_default_toolbar);
    setSupportActionBar(mToolBar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.screen_default_container, HomeFragment.newInstance(),"fragment_home").commit();
    }

    Navigation_logined navigation_logined=new Navigation_logined();
    DrawerLayout d=(DrawerLayout)findViewById(R.id.drawer_layout);
    navigation_logined.setUp(d,mToolBar);

}
}

The logcat shows the exception on isLayoutRtl() function of DrawerArrowDrawableToggle subclass of ActionBarDrawerToggle.

Logcat

04-16 00:16:20.346  18198-18198/com.xyz.abc.pqr E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.support.v7.app.ActionBarDrawerToggle$DrawerArrowDrawableToggle.isLayoutRtl(ActionBarDrawerToggle.java:484)
        at android.support.v7.app.DrawerArrowDrawable.draw(DrawerArrowDrawable.java:99)
        at android.widget.ImageView.onDraw(ImageView.java:1002)
        at android.view.View.draw(View.java:13614)
        at android.view.View.getDisplayList(View.java:12565)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.View.draw(View.java:13338)
        at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
        at android.view.View.draw(View.java:13617)
        at android.view.View.getDisplayList(View.java:12565)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.View.draw(View.java:13338)
        at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
        at android.view.View.getDisplayList(View.java:12563)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.View.draw(View.java:13338)
        at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
        at android.view.View.draw(View.java:13617)
        at android.view.View.getDisplayList(View.java:12565)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.View.draw(View.java:13338)
        at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
        at android.view.View.getDisplayList(View.java:12563)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.View.draw(View.java:13338)
        at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
        at android.view.View.getDisplayList(View.java:12563)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.View.draw(View.java:13338)
        at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
        at android.view.View.getDisplayList(View.java:12563)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.View.draw(View.java:13338)
        at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
        at android.view.View.getDisplayList(View.java:12563)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.View.draw(View.java:13338)
        at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
        at android.view.View.draw(View.java:13617)
        at android.widget.FrameLayout.draw(FrameLayout.java:467)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2458)
        at android.view.View.getDisplayList(View.java:12565)
        at android.view.View.getDisplayList(View.java:12609)
        at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1187)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2313)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2185)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1993)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4674)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4960)
        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:1038)
        at com.android.internal.os.Zygot

Solution

  • You are missing some code in setup function

        layers = getResources().getStringArray(R.array.plans_array);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        drawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, android.R.id.text1,layers));
    

    where text1 is textview inside drawer_list_item layout and plans_array is array of strings of list items