当前位置: 首页 > 工具软件 > eC-lang > 使用案例 >

java.lang.IllegalStateException: Fragment no longer exists for key f0: unique id 8b73484e-f6ef-43c6

尉迟冯浩
2023-12-01
java.lang.IllegalStateException: Fragment no longer exists for key f0: unique id 8b73484e-f6ef-43c6-b7f7-17c1df3ee85b
        at androidx.fragment.app.FragmentManager.getFragment(FragmentManager.java:772)
        at androidx.fragment.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:324)
        at androidx.viewpager.widget.ViewPager.onRestoreInstanceState(ViewPager.java:1461)
        at android.view.View.dispatchRestoreInstanceState(View.java:18072)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3803)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3809)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3809)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3809)
        at android.view.View.restoreHierarchyState(View.java:18050)
        at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:573)
        at androidx.fragment.app.FragmentStateManager.restoreViewState(FragmentStateManager.java:356)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1189)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:442)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:167)
        at android.app.ActivityThread.main(ActivityThread.java:7146)
        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:820)

代码情况:

在Activity里面把两个Fragment加载到FrameLayout里面,切换显示
每个Fragment里面有Viewpager包装两个Fragment

即:Fragment嵌套ViewPager嵌套Fragment
切换回第一个外层Fragment的时候报上述错误

bug的意思是

  • Fragment为空
  • Fragment已被销毁

解决办法:

ViewPager的FragmentAdapter里面,不要使用FragmentStatePagerAdapter,而是让里外层Fragment的Adaper继承> FragmentPagerAdapter。

  • 在报错的Fragment的Adapter中重写以下方法,返回空
 var diskPagerAdapter =
                object : FragmentLazyStatePageAdapter(childFragmentManager, pageTitles) {
                    override fun getItem(position: Int): Fragment {
                       ...
                    }

                    //添加代码
                    override fun saveState(): Parcelable? {
                        return null
                    }
                }

结果:没有崩溃,显示正常 暂时解决

参考:

Android Bug : Fragment no longer exists for key f0: index 0_写代码的仓颉-CSDN博客

Fragment no longer exists for key f0: index 0_eydwyz的专栏-CSDN博客

 类似资料:

相关阅读

相关文章

相关问答