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

AndroidAndroid FragmentsSurfaceview

Android Problem Overview


I am getting this error. I tried many solutions but I coudn't solve this. Help me! I need to add the surface view and the button to the activity using fragments.

CamActivity.java:

public class CamActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cam);
		
		FragmentManager fm = getSupportFragmentManager();
		
		Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
		
		if(fragment == null) {
			fragment = new CamFragment();
			fm.beginTransaction()
			.add(R.id.fragmentContainer, fragment)
			.commit();
		}
    }
}

CamFragment.java:

public class CamFragment extends Fragment {

	private static final String TAG = "CamFragment";
	
	private Camera mCamera;
	private SurfaceView mSurfaceView;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
	View v = inflater.inflate(R.layout.camera_fragment, parent);
	
	Button capturePic = (Button)v.findViewById(R.id.img_capture);
	capturePic.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			getActivity().finish();
		}
	});
	
	mSurfaceView = (SurfaceView)v.findViewById(R.id.surfaceView1);
	return v;
}

}

Error:

04-18 13:24:12.735: E/AndroidRuntime(6321): FATAL EXCEPTION: main
04-18 13:24:12.735: E/AndroidRuntime(6321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pack.camdictionary/com.pack.camdictionary.CamActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1728)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1747)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.app.ActivityThread.access$1500(ActivityThread.java:155)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.os.Handler.dispatchMessage(Handler.java:130)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.os.Looper.loop(SourceFile:351)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.app.ActivityThread.main(ActivityThread.java:3814)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at java.lang.reflect.Method.invokeNative(Native Method)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at java.lang.reflect.Method.invoke(Method.java:538)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at dalvik.system.NativeStart.main(Native Method)
04-18 13:24:12.735: E/AndroidRuntime(6321): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.view.ViewGroup.addViewInner(ViewGroup.java:2007)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.view.ViewGroup.addView(ViewGroup.java:1902)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.view.ViewGroup.addView(ViewGroup.java:1859)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.view.ViewGroup.addView(ViewGroup.java:1839)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1166)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.app.Activity.performStart(Activity.java:3837)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
04-18 13:24:12.735: E/AndroidRuntime(6321): 	... 11 more

Android Solutions


Solution 1 - Android

Try to replace

View v = inflater.inflate(R.layout.camera_fragment, parent);

With

View v = inflater.inflate(R.layout.camera_fragment, parent, false);

or

View v = inflater.inflate(R.layout.camera_fragment, null);

Solution 2 - Android

This question had already been answered but still I would like to add the reason for adding false as the third parameter.

>The inflate() method takes three arguments: > > - The resource ID of the layout you want to inflate. > - The ViewGroup to be the parent of the inflated layout. Passing the container is important in order for the system to apply layout parameters to the root view of the inflated layout, specified by the parent view in which it's going. > - A boolean indicating whether the inflated layout should be attached to the ViewGroup (the second parameter) during inflation. (In this case, this is false because the system is already inserting the inflated layout into the container—passing true would create a redundant view group in the final layout.)

Source: http://developer.android.com/guide/components/fragments.html

Solution 3 - Android

In my case, I was doing the setTransition() during the FragmentTransaction. Below code worked for me:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        if(this::rootView.isInitialized){
            if(rootView.getParent() != null){
                (rootView.getParent() as ViewGroup).endViewTransition(rootView)
            }
            return this.rootView

        }
       
        ...

}

This probably happens when you repeatedly switch between fragments, that the view does not get time to really detach from the parent due to the ongoing transition.

Solution 4 - Android

I am using API 26.0.2 and in my case, the crash was happening because of custom animations set for my fragments. Commenting out the call to set the custom animation, fixed the issue.

fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);

Solution 5 - Android

I also met this problem, and this is my solution. it's very easy

View v;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (v == null) {
        v = inflater.inflate(R.layout.camera_fragment, container);
    } else {
        ViewGroup parent = (ViewGroup) v.getParent();
        if (parent != null) {
            parent.removeView(v);
        }
    }
    return v;
}

Hope to be useful to you

Solution 6 - Android

Specify Id for parent and child element, especially for custom views, in xml file may solve problem.

Solution 7 - Android

Just for your reference. It seems to be an issue in the Android SDK.

I have the same situation like @Satheesh had with the SDK version: 27.0.2 and trying to remove the setCustomAnimation seems works. But to remove the setCustomAnimations requires lots of change to make for me, I'd prefer to keep the custom animation. What I have done is to upgrade the SDK to the latest version which is 27.1.1 for now. And it works for me.

Solution 8 - Android

Might be Helpfull

           View customView1;
           inflater= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           customView1= inflater.inflate(R.layout.edit_custom_alert_window, null);     
           try {
                if(customView2.getParent()!=null)
                    ((ViewGroup)customView2.getParent()).removeView(customView2);
            }catch (Exception e){

            }

Solution 9 - Android

I had navigation in my code and this error was shown when I clicked back button from one of the fragments. It solved when in the nav_graph I put popUpTo in related actions. Some actions were done but didn't popup anything.

Solution 10 - Android

Error by this line:

View v = inflater.inflate(R.layout.camera_fragment, parent);

Try to replace with:

View v = inflater.inflate(R.layout.camera_fragment, null);

Solution 11 - Android

So I had a case in which my fragment consisted of nested sub-fragments (Bottom Navigation). Let's say I have Fragment A (which contains sub fragments) and another basic Fragment B. So while navigating from A to B, when I pressed back button, I was facing this error - The specified child already has a parent. You must call removeView() on the child's parent first.

And I found out that the reason was in the error statement only. So in OnDestroyView of Fragment A, I did the following -

override fun onDestroyView() {
    (view?.parent as? ViewGroup)?.removeAllViews()
    super.onDestroyView()
}

And it worked like charm.

Solution 12 - Android

try this.

 ((CamActivity)getActivity()).finish();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVivekView Question on Stackoverflow
Solution 1 - Androiduser3390963View Answer on Stackoverflow
Solution 2 - AndroidBharat Kul RatanView Answer on Stackoverflow
Solution 3 - AndroidIrshuView Answer on Stackoverflow
Solution 4 - AndroidSatheeshView Answer on Stackoverflow
Solution 5 - AndroidAnhSangView Answer on Stackoverflow
Solution 6 - AndroidSayed Abolfazl FatemiView Answer on Stackoverflow
Solution 7 - AndroidPai-Hsiang HuangView Answer on Stackoverflow
Solution 8 - AndroidMuhaiminur RahmanView Answer on Stackoverflow
Solution 9 - AndroidMMGView Answer on Stackoverflow
Solution 10 - AndroidMark NashatView Answer on Stackoverflow
Solution 11 - AndroidMayank KhandelwalView Answer on Stackoverflow
Solution 12 - Androidmaddy dView Answer on Stackoverflow