ViewPager 和Fragment异常 java.lang.IllegalStateException: Fragment no longer exists for key

异常:java.lang.IllegalStateException: Fragment no longer exists for key f0: unique id 0aa277f1-a562-499e-b765-08317f40c04c

Process: com.xxx.xxx, PID: 15243
    java.lang.IllegalStateException: Fragment no longer exists for key f0: unique id 0aa277f1-a562-499e-b765-08317f40c04c
        at androidx.fragment.app.FragmentManager.getFragment(FragmentManager.java:960)
        at androidx.fragment.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:328)
        at androidx.viewpager.widget.ViewPager.onRestoreInstanceState(ViewPager.java:1461)
        at android.view.View.dispatchRestoreInstanceState(View.java:21866)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4047)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4053)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4053)
        at android.view.View.restoreHierarchyState(View.java:21844)
        at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:639)
        at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:3010)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:3001)
        at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:580)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:285)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
        at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:249)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
        at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:26791)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:995)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:26791)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7066)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:26791)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7066)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:26791)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7066)
        at com.android.internal.policy.DecorView.measureChildWithMargins(DecorView.java:3504)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:999)
        at android.view.View.measure(View.java:26791)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:4253)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2841)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3175)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2550)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9652)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1480)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1488)
        at android.view.Choreographer.doCallbacks(Choreographer.java:1111)
        at android.view.Choreographer.doFrame(Choreographer.java:1016)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1463)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:223)
        at android.os.Looper.loop(Looper.java:324)
        at android.app.ActivityThread.main(ActivityThread.java:8470)
        at java.lang.reflect.Method.invoke(Native Method)
2023-08-03 14:37:48.788 15243-15243/? E/AndroidRuntime:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1059)

解决方法两种

① 设置Viewpager的isSaveEnabled

 viewPager.setSaveEnabled(false);

② 重写FragmentStatePagerAdapter中的saveState方法

    @Override
    public Parcelable saveState() {
        return  null;
    }

好久没用过Viewpager了,记录一下。

你可能感兴趣的:(java,Android,viewPager,Fragment)