将androidx.appcompat:appcompat更新到版本1.1.0后崩溃IllegalStateException

时间:2019-12-05 16:06:20

标签: android android-fragments crash android-appcompat androidx

将androidx.appcompat:appcompat从1.0.0版本更新为1.1.0版本后,在片段之间切换时我的应用崩溃了:

我的片段延伸

androidx.fragment.app.Fragment implements ComponentCallbacks, OnCreateContextMenuListener, LifecycleOwner,
        ViewModelStoreOwner, SavedStateRegistryOwner



V/FA: Inactivity, disconnecting from the service
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.letyshops.debug, PID: 10934
    java.lang.IllegalStateException: Restarter must be created only during owner's initialization stage
        at androidx.savedstate.SavedStateRegistryController.performRestore(SavedStateRegistryController.java:58)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2585)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:434)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
D/FA: Logging event (FE): ux_analytics_event, Bundle[{trigger_method_name=onDrawerClosed, online=1, ga_event_origin(_o)=app, ga_screen_class(_sc)=MainActivity, ga_screen_id(_si)=-8250529364285859535, ga_screen(_sn)=Help_Screen, trigger_name=R.id.slide_menu_notifications, trigger_type=CLICK, targetName=NotificationsFragment, context=MainActivity}]
V/FA: Connecting to remote service

要在片段之间切换,我在BaseActivity中使用以下代码:

public void showFragment(int containerViewId, Fragment fragment, boolean needToRefreshBackStack) {

        if (needToRefreshBackStack) {
            int indexToDelete = (fragment.getClass().getSimpleName()
                    .equals(fragmentManager.getBackStackEntryAt(0).getName())) ? 0 : 1;
            int backStackFragmentsCount = fragmentManager.getBackStackEntryCount();
                for (int i = backStackFragmentsCount - 1; i >= indexToDelete; i--) {
                    int backStackId = fragmentManager.getBackStackEntryAt(i).getId();
                    fragmentManager.popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                }
        }
        previousFragment = currentFragment;
        currentFragment = fragment;

        String fragmentTag = fragment.getClass().getSimpleName();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

        if (previousFragment != null) {
            fragmentTransaction.hide(previousFragment);
        }
        fragmentTransaction.add(containerViewId, fragment, fragmentTag)
                .addToBackStack(fragmentTag)
                .commitAllowingStateLoss();
    }

    public void refreshBackStackToShopsPage() {
        int backStackFragmentsCount = fragmentManager.getBackStackEntryCount();
        for (int i = backStackFragmentsCount - 1; i > 0; i--) {
            int backStackId = fragmentManager.getBackStackEntryAt(i).getId();
            fragmentManager.popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }

    protected void showFragmentWithoutBackStack(int containerViewId, Fragment fragment) {
        previousFragment = currentFragment;
        currentFragment = fragment;
        String fragmentTag = fragment.getClass().getSimpleName();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        if (previousFragment != null) {
            fragmentTransaction.hide(previousFragment);
        }
        fragmentTransaction.add(containerViewId, fragment, fragmentTag)
                .commitAllowingStateLoss();
    }

    protected void showFragmentWithoutBackStackImmediately(int containerViewId, Fragment fragment) {
        previousFragment = currentFragment;
        currentFragment = fragment;
        String fragmentTag = fragment.getClass().getSimpleName();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        if (previousFragment != null) {
            fragmentTransaction.remove(previousFragment);
        }
        fragmentTransaction.add(containerViewId, fragment, fragmentTag)
                .commitNowAllowingStateLoss();
    }

0 个答案:

没有答案