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

原文: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.handleRelaunchActivity(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方法

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

上一篇:android自定义!阿里大神最佳总结Flutter进阶学习笔记,成功入职腾讯


下一篇:Android事件分发机制三:事件分发工作流程