Android nullpointerexception in ActionBarDrawerToggle.syncState ()

I started my application a few days ago, and sometimes I can crash by leaving the application using the back button and then opening it after a few hours. This is not always the case, in fact it is completely random, and 99 out of 100 times work fine, but I obviously want 100.

This is the corresponding bit of my activity:

public class MainActivity extends AppCompatActivity
{
    private ActionBarDrawerToggle drawerToggle;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (findViewById(R.id.fragment_container) != null) {
            if (savedInstanceState != null) {
                return;
            }

            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawerToggle = new ActionBarDrawerToggle(
                this,
                drawerLayout,
                R.string.drawer_open,
                R.string.drawer_close
            ) {
                @Override
                public void onDrawerClosed(View view)
                {
                    super.onDrawerClosed(view);
                    invalidateOptionsMenu();
                }

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

                @Override
                public void onDrawerSlide(View drawerView, float slideOffset)
                {
                    super.onDrawerSlide(drawerView, 0);
                }
            };
            drawerLayout.setDrawerListener(drawerToggle);
       }
}

   @Override
   protected void onPostCreate(Bundle savedInstanceState)
   {
      super.onPostCreate(savedInstanceState);
      drawerToggle.syncState();
   }
}

The exception is the string drawerToggle.syncState();. Trace:

`java.lang.RuntimeException: Unable to start activity ComponentInfo{io.my.my/io.my.my.ui.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.syncState()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
    at android.app.ActivityThread.access$900(ActivityThread.java:177)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5942)
    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:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.syncState()' on a null object reference
    at io.my.my.ui.MainActivity.onPostCreate(MainActivity.java:130)
    at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1200)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2675)
    ... 10 more`

I could solve this by running a line in if (drawerToggle != null)? But I would like to know why this is happening, and if I am wrong. It is also quite difficult to verify because it is so random.

: , drawerLayout, drawerToggle. https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html:

Call syncState() from your Activity onPostCreate to synchronize the indicator with the state of the linked DrawerLayout after onRestoreInstanceState has occurred.

onPostCreate. onStart.

, :

   if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }
   }

, , drawerToggle null... ?

+4

Source: https://habr.com/ru/post/1607073/


All Articles