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

 public static void startActivityWithClearTop(Context context, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

To

public static void startActivity(Context context, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        context.startActivity(intent);
    }

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

Then I change

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

To

public static 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"