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

Original: https://blog.csdn.net/lxd_love_lgc/article/details/105650993
I encountered an error when doing ViewPage+Fragment switching between horizontal and vertical screens. It took a long time to solve it, so record:
The specified child already has a parent. You must call removeView() on the child's parent first.

The error message is as follows:
 

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",

The main reason for the error is this sentence:  java.lang.IllegalStateException: 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.

Two solutions

The first:  get the parent class of view and remove
 

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);
            }
        }
    }     

But tried this way不一定奏效

The second method:  add a configuration item to
the activity that the Fragment depends onAndroidManifest.xml
 

    android:configChanges="orientation|keyboardHidden|screenSize" />

The role of this configuration item:  Activity does not call the onCreate method again when switching between horizontal and vertical screens

Pro test is effective,
I hope it will be helpful to students who also encounter this problem

Guess you like

Origin blog.csdn.net/az44yao/article/details/112755060