在 fragmenStatePagerAdapter 的嵌套片段中,非法状态异常-指定的子级已经有父级
Posted
技术标签:
【中文标题】在 fragmenStatePagerAdapter 的嵌套片段中,非法状态异常-指定的子级已经有父级【英文标题】:illegalstateexception-the-specified-child-already-has-a-parent inside nested fragment on fragmenStatePagerAdapter 【发布时间】:2020-11-04 21:19:07 【问题描述】:我在嵌套片段中有一个材质选项卡布局和 androidx 视图寻呼机。当我导航到不同的片段或活动,然后使用 tablayot 返回我的片段时,它会因以下跟踪而崩溃
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:5106)
at android.view.ViewGroup.addView(ViewGroup.java:4935)
at androidx.viewpager.widget.ViewPager.addView(ViewPager.java:1485)
at android.view.ViewGroup.addView(ViewGroup.java:4875)
at android.view.ViewGroup.addView(ViewGroup.java:4848)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:326)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
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:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
这是我使用 tablayot 和片段 viewPager 的片段
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val adapter=ViewPagerAdapter(childFragmentManager)
adapter.addFragment(TabOneFragment)
adapter.addFragment(TabTwoFragment)
adapter.addFragment(TabThreeFragment)
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
我尝试了所有指向该跟踪的答案,但没有一个有效。 如果我将活动的 fragmentManager 传递给 ViewPagerAdapter 不会发生崩溃,但在这种情况下它会泄漏内存。 有什么想法吗?
【问题讨论】:
【参考方案1】:您是否尝试过删除 onDestroyView 中的视图? (Like this)
对不起,我没有足够的代表来写 cmets【讨论】:
以上是关于在 fragmenStatePagerAdapter 的嵌套片段中,非法状态异常-指定的子级已经有父级的主要内容,如果未能解决你的问题,请参考以下文章
NOIP 2015 & SDOI 2016 Round1 & CTSC 2016 & SDOI2016 Round2游记