Question

I am getting a force close reported from my app users that gives this error:

java.lang.NullPointerException
       at com.beerportfolio.beerportfoliopro.MainDrawer2.onSaveInstanceState(MainDrawer2.java:160)
       at android.app.Activity.performSaveInstanceState(Activity.java)
       at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java)
       at android.app.ActivityThread.performStopActivityInner(ActivityThread.java)
       at android.app.ActivityThread.handleStopActivity(ActivityThread.java)
       at android.app.ActivityThread.access$1100(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
       at dalvik.system.NativeStart.main(NativeStart.java)

Line 160 is this:

outState.putInt(STATE_CURRENT_NAV, mCurrentNavItem.ordinal());

which come from this method:

@Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_CURRENT_NAV, mCurrentNavItem.ordinal());
    }

should I just use an if statement to check if either STATE_CURRENT_NAV or mCurrentNavItem.ordinal() are NULL and if they are then not execute the outState.putInt?

Était-ce utile?

La solution

outState is contractually not null ergo your Nav Item must be null. Either null check before attempting to save or init the variable so it can not be null at the time of saving state.

onSaveInstanceState(Bundle)

Autres conseils

It could be outState or mCurrentNavItem that is null. I dont think STATE_CURRENT_NAV is null as thats a predefined string right?

If outState is null, create it before using it.

The enum for mCurrentNavItem should have a default value like NONE, then you can initialise the member variable to that and ensure it is never null.

You could also check if mCurrentNavItem is null first and then save a default value to the bundle if so.

Hope that helps...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top