NullPointerException: Attempt to invoke virtual me

2020-04-08 10:07发布

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 ComponentInfo{com.bookmark.money/com.zoostudio.moneylover.ui.ActivityBase}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4122)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
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:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
at android.view.View.dispatchRestoreInstanceState(View.java:13796)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3016)
at android.view.View.restoreHierarchyState(View.java:13779)
at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.restoreInstanceState(NavigationMenuPresenter.java:592)
at android.support.design.internal.NavigationMenuPresenter.onRestoreInstanceState(NavigationMenuPresenter.java:191)
at android.support.v7.view.menu.MenuBuilder.dispatchRestoreInstanceState(MenuBuilder.java:355)
at android.support.v7.view.menu.MenuBuilder.restorePresenterStates(MenuBuilder.java:367)
at android.support.design.widget.NavigationView.onRestoreInstanceState(NavigationView.java:201)
at android.view.View.dispatchRestoreInstanceState(View.java:13801)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3016)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3022)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3022)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3022)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3022)
at android.view.View.restoreHierarchyState(View.java:13779)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2053)
at android.app.Activity.onRestoreInstanceState(Activity.java:1027)
at android.app.Activity.performRestoreInstanceState(Activity.java:982)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1168)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
... 11 more

Please help me if you have any ideas! Thanks for your time.

ps: code ActivityBase involve NavigationView

private void initNavigationMenu() {
    Menu menu = mNavigationView.getMenu();
    menu.clear();

    AccountItem.Policy policy = MoneyAccountHelper.getCurrentAccount(getApplicationContext()).getPolicy();

    menu.add(MENU_GROUP_1, MENU_CASHBOOK, MENU_CASHBOOK, R.string.navigation_cashbook)
            .setIcon(R.drawable.ic_cashbook)
            .setCheckable(true)
            .setEnabled(policy.transaction.view)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.transaction.view);


    menu.add(MENU_GROUP_1, MENU_DEBTS, MENU_DEBTS, R.string.navigation_debt_manager)
            .setIcon(R.drawable.ic_debts)
            .setCheckable(true)
            .setEnabled(policy.debt.view)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.debt.view);

    menu.add(MENU_GROUP_1, MENU_TRENDS, MENU_TRENDS, R.string.statistic_trends)
            .setIcon(R.drawable.ic_trends)
            .setCheckable(true);

    menu.add(MENU_GROUP_1, MENU_CATEGORIES, MENU_CATEGORIES, R.string.navigation_category_manager)
            .setIcon(R.drawable.ic_category_manager)
            .setEnabled(policy.category.view)
            .setCheckable(true)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.category.view);

    menu.add(MENU_GROUP_PLANNING, TRY_LINKED_WALLET, TRY_LINKED_WALLET, R.string.connect_to_bank)
            .setChecked(false)
            .setIcon(R.drawable.ic_link);

    if (MoneyConfig.ENABLE_SCAN_RECEIPT) {
        menu.add(MENU_GROUP_PLANNING, MENU_SCAN_RECEIPT, MENU_SCAN_RECEIPT, R.string.scan_receipt_title)
                .setIcon(R.drawable.ic_scan_receipt)
                .setCheckable(false)
                .setEnabled(policy.transaction.add)
                .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.transaction.add);
    }
    menu.add(MENU_GROUP_PLANNING, MENU_BUDGETS, MENU_BUDGETS, R.string.navigation_budget)
            .setIcon(R.drawable.ic_budget)
            .setCheckable(true)
            .setEnabled(policy.budget.view)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.budget.view);

    menu.add(MENU_GROUP_PLANNING, MENU_SAVINGS, MENU_SAVINGS, R.string.navigation_campaign)
            .setIcon(R.drawable.ic_savings)
            .setCheckable(true)
            .setEnabled(policy.saving.view)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.saving.view);

    menu.add(MENU_GROUP_PLANNING, MENU_EVENTS, MENU_EVENTS, R.string.navigation_event)
            .setIcon(R.drawable.ic_events)
            .setCheckable(true)
            .setEnabled(policy.event.view)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.event.view);

    menu.add(MENU_GROUP_PLANNING, MENU_TRAVEL, MENU_TRAVEL, R.string.navigation_travel_mode)
            .setIcon(R.drawable.ic_travel_mode)
            .setEnabled(policy.event.view)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.event.view);

    addSwitchForTravel(MoneyPreference.App().getTravelModeStatus());

    menu.add(MENU_GROUP_PLANNING, MENU_BILLS, MENU_BILLS, R.string.navigation_bill)
            .setIcon(R.drawable.ic_bills)
            .setCheckable(true)
            .setEnabled(policy.bill.view)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.bill.view);

    menu.add(MENU_GROUP_PLANNING, MENU_RECURRING, MENU_RECURRING, R.string.repeat_transaction_manager)
            .setIcon(R.drawable.ic_recurring_transaction)
            .setCheckable(true)
            .setEnabled(policy.recurring.view)
            .setVisible(!MoneyConfig.NAVIGATION_ITEM_GONE_ENABLE || policy.recurring.view);

    menu.add(MENU_GROUP_ML, MENU_STORE, MENU_STORE, R.string.navigation_store)
            .setCheckable(false)
            .setIcon(R.drawable.ic_store);

    menu.add(MENU_GROUP_ML, MENU_BLOG, MENU_BLOG, getString(R.string.explore_money_lover))
            .setCheckable(false)
            .setIcon(R.drawable.ic_app_icon);

    if (MoneyApplication.MODE == MoneyApplication.MODE_OFFLINE) {
        menu.removeItem(MENU_PROFILE);
        menu.add(MENU_GROUP_ML, MENU_LOGIN, MENU_LOGIN, R.string.login_or_register)
                .setCheckable(false)
                .setIcon(R.drawable.ic_cloud);
    } else {
        menu.removeItem(MENU_LOGIN);
        menu.add(MENU_GROUP_ML, MENU_PROFILE, MENU_PROFILE, R.string.cloud_manager_title)
                .setCheckable(false)
                .setIcon(R.drawable.ic_cloud);
    }

    menu.add(MENU_GROUP_OTHERS, MENU_TOOLS, MENU_TOOLS, R.string.navigation_tools)
            .setCheckable(false)
            .setIcon(R.drawable.ic_tools);


    menu.add(MENU_GROUP_OTHERS, MENU_HELP_FEEDBACK, MENU_HELP_FEEDBACK, R.string.cashbook_contentdescription_help_support)
            .setCheckable(false)
            .setIcon(R.drawable.ic_help);

    menu.add(MENU_GROUP_OTHERS, MENU_SETTINGS, MENU_SETTINGS, R.string.navigation_settings)
            .setCheckable(false)
            .setIcon(R.drawable.ic_settings);

    if (!PermissionUtils.checkBuyFullFeature(this)) {
        menu.add(MENU_GROUP_OTHERS, 19, 19, "");
    }

    try {
        listBadge = BadgeUtils.getArrayListBadgeFromPref();
    } catch (JSONException e) {
        Logger.e(TAG, "K bóc dc json new item");
        e.printStackTrace();
    }

    showBadgeNewItem(listBadge);
}

标签: android
2条回答
家丑人穷心不美
2楼-- · 2020-04-08 10:17

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.

查看更多
看我几分像从前
3楼-- · 2020-04-08 10:27

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.

查看更多
登录 后发表回答