Skip to content Skip to sidebar Skip to footer

Nullpointerexception: Attempt To Invoke Virtual Method 'java.lang.object Android.util.sparsearray.get(int)' On A Null Object Reference

I used this android support library ver 25.0.1 for my project I met errors on some device on some time In logcat: //logcat java.lang.RuntimeException: Unable to start activity Comp

Solution 1:

When trying to restore the state of your NavigationView, the system found a new menu item (with a new item id) with an Action View than was not there when saving the state. It then tries to restore the state of that Action View by passing a null state.

To avoid this, make sure your menu items using action views do not change dynamically or don't use NavigationView.

Solution 2:

LoginActivity : login screen

MainActivityDrawer has a lot Drawer menu Fragment

I got that error when startActivity from Login to MainActivityDrawer with flag Intent clearTop.

Then I change clear

publicstaticvoidstartActivityWithClearTop(Context context, Class<?> cls) {
        Intentintent=newIntent();
        intent.setClass(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

To

publicstaticvoidstartActivity(Context context, Class<?> cls) {
        Intentintent=newIntent();
        intent.setClass(context, cls);
        context.startActivity(intent);
    }

After that I get error Fragment already active and state has been saved

Then I change

publicstatic FriendFragment getInstance() {
        Bundle args = new Bundle();
        if (friendFragment == null){
            friendFragment  = new FriendFragment();
        }
        friendFragment.setArguments(args);
        return friendFragment;
    }

To

publicstatic FriendFragment getInstance() {
        Bundle args = new Bundle();
        if (friendFragment == null){
            friendFragment  = new FriendFragment();
            friendFragment.setArguments(args);
        }
        return friendFragment;
    }

Now it's working as well.

Post a Comment for "Nullpointerexception: Attempt To Invoke Virtual Method 'java.lang.object Android.util.sparsearray.get(int)' On A Null Object Reference"