The specified child already has a parent. You must call removeView() on the child‘s parent first.

The specified child already has a parent. You must call removeView() on the child‘s parent first.,第1张

概述原文:https://blog.csdn.net/lxd_love_lgc/article/details/105650993之前在做ViewPage+Fragment横竖屏切换时遇到报错了,搞了好久才解决,所以记录一下:Thespecifiedchildalreadyhasaparent.YoumustcallremoveView()onthechild’sparentfirst.报错信息如下: ja

原文:https://blog.csdn.net/lxd_love_lgc/article/details/105650993
之前在做VIEwPage+Fragment 横竖屏切换时 遇到报错了,搞了好久才解决,所以记录一下:
The specifIEd child already has a parent. You must call removeVIEw() on the child’s parent first.

报错信息如下:
 

java.lang.IllegalStateException: The specifIEd child already has a parent. You must call removeVIEw() on the child's parent first.	at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:3194)	at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)	at androID.app.ActivityThread.handle@R_23_4404@Activity(ActivityThread.java:5368)	at androID.app.ActivityThread.-wrap19(UnkNown Source:0)	at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1897)	at androID.os.Handler.dispatchMessage(Handler.java:108)	at androID.os.Looper.loop(Looper.java:166)	at androID.app.ActivityThread.main(ActivityThread.java:7425)	at java.lang.reflect.Method.invoke(Native Method)	at com.androID.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)	at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:921)Caused by: 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:4976)	at androID.vIEw.VIEwGroup.addVIEw(VIEwGroup.java:4807)	at androIDx.vIEwpager.Widget.VIEwPager.addVIEw(VIEwPager.java:1485)	at androID.vIEw.VIEwGroup.addVIEw(VIEwGroup.java:4747)	at androID.vIEw.VIEwGroup.addVIEw(VIEwGroup.java:4720)	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.FragmentController.dispatchActivityCreated(FragmentController.java:247)	at androIDx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)	at androID.app.Instrumentation.callActivityOnStart(Instrumentation.java:1339)	at androID.app.Activity.performStart(Activity.java:7392)	at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:3157)	... 10 more","40","0",

主要报错原因的就是这句: java.lang.IllegalStateException: The specifIEd child already has a parent. You must call removeVIEw() on the child’s parent first.
指定的孩子已经有一个父母。您必须先在孩子的父母上调用removeVIEw()。

两种解决办法

第一种: 获取vIEw 的父类 并移除
 

private VIEw vIEw;    @Nullable    @OverrIDe    public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) {        if (vIEw == null) {            vIEw = inflater.inflate(R.layout.message_center_fragment, container, false);        }        VIEwGroup parent = (VIEwGroup) vIEw.getParent();        if (parent != null) {            parent.removeVIEw(vIEw);        }            @OverrIDe    public voID onDestroyVIEw() {        super.onDestroyVIEw();        if (vIEw != null) {            VIEwGroup parentVIEw = (VIEwGroup) vIEw.getParent();            if (parentVIEw != null) {                parentVIEw.removeVIEw(vIEw);            }        }    }     

不过这种方式试了一下不一定奏效

第二种方法:
在Fragment 所以依赖的activity 的AndroIDManifest.xml 中加一个配置项
 

    androID:configChanges="orIEntation|keyboardHIDden|screenSize" />

该配置项的作用: activity在横竖屏切换的时候不重新调用onCreate方法

亲测有效
希望对同样遇到这个问题的同学有所帮助

总结

以上是内存溢出为你收集整理的The specified child already has a parent. You must call removeView() on the child‘s parent first.全部内容,希望文章能够帮你解决The specified child already has a parent. You must call removeView() on the child‘s parent first.所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053186.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存