The specified child already has a parent. You must first call removeView () for the parent parent (Android)

In my application, I often have to switch between two layouts. The error occurs in the layout below.

When my layout is called for the first time, no error occurs, and everything is in order. When I then call another layout (empty) and then call my layout a second time, it gives me the following error:

> FATAL EXCEPTION: main > java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child parent first. 

My layout code looks like this:

  tv = new TextView(getApplicationContext()); // are initialized somewhere else et = new EditText(getApplicationContext()); // in the code private void ConsoleWindow(){ runOnUiThread(new Runnable(){ @Override public void run(){ // MY LAYOUT: setContentView(R.layout.activity_console); // LINEAR LAYOUT LinearLayout layout=new LinearLayout(getApplicationContext()); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); // TEXTVIEW layout.addView(tv); // <========== ERROR IN THIS LINE DURING 2ND RUN // EDITTEXT et.setHint("Enter Command"); layout.addView(et); } } } 

I know this question has already been asked, but in my case it did not help.

+107
java android android-edittext textview parent-child
Jan 21 '15 at 15:50
source share
15 answers

The error message says what you should do.

 // TEXTVIEW if(tv.getParent() != null) { ((ViewGroup)tv.getParent()).removeView(tv); // <- fix } layout.addView(tv); // <========== ERROR IN THIS LINE DURING 2ND RUN // EDITTEXT 
+270
Jan 21 '15 at 15:53
source share

I came here while looking for an error with my recyclerview, but the solution did not work (obviously). I wrote a reason and solution for it in a recyclerview case. Hope this helps someone.

The error occurs if the following method is used in onCreateViewHolder() :

 layoutInflater = LayoutInflater.from(context); return new VH(layoutInflater.inflate(R.layout.single_row, parent)); 

Instead, he should be

 return new VH(layoutInflater.inflate(R.layout.single_row, null)); 
+38
Nov 04 '16 at 10:58
source share

just pass attachtoroot argument as false

 View view = inflater.inflate(R.layout.child_layout_to_merge, parent_layout, false); 
+33
Nov 01 '17 at 21:35
source share

I got this message trying to fix a fragment using attaching to the root in true instead of false, like this:

 return inflater.inflate(R.layout.fragment_profile, container, true) 

After execution:

 return inflater.inflate(R.layout.fragment_profile, container, false) 

It worked.

+8
Jun 18 '18 at 20:03
source share

If another solution does not work, like:

 View view = inflater.inflate(R.layout.child_layout_to_merge, parent_layout, false); 

to check that you are returning from the onCreateView fragment, is it a single view or a viewing group? in my case, I had a viewpager in the root of the xml fragment, and I returned the viewpager when I added the viewgroup to the layout, I did not update that I should return the viewgroup now, not the viewpager (view).

+3
May 31 '18 at 8:12
source share

In my case, the problem was caused by the fact that I was inflating the parent view with the <merge> layout. In this case, addView() failed.

 View to_add = inflater.inflate(R.layout.child_layout_to_merge, parent_layout, true); // parent_layout.addView(to_add); // THIS CAUSED THE CRASH 

Removing addView() helped solve the problem.

+2
Mar 20 '17 at 12:50
source share

frameLayout.addView (bannerAdView); <----- if you get an error in this line, do as shown below ..

 if (bannerAdView.getParent() != null) ((ViewGroup) bannerAdView.getParent()).removeView(bannerAdView); frameLayout.addView(bannerAdView); <------ now added view 
+2
Oct 25 '18 at 11:45
source share

I found another fix:

 if (mView.getParent() == null) { myDialog = new Dialog(MainActivity.this); myDialog.setContentView(mView); createAlgorithmDialog(); } else { createAlgorithmDialog(); } 

Here I have only an if statement that checks whether the parent had a view and did not create a new dialog, set the ContentView and show the dialog in my createAlgorithmDialog () method.

It also sets the positive and negative buttons (ok and cancel buttons) using onClickListeners.

+1
Feb 17 '17 at 2:19 on
source share

The code below solved this for me:

 @Override public void onDestroyView() { if (getView() != null) { ViewGroup parent = (ViewGroup) getView().getParent(); parent.removeAllViews(); } super.onDestroyView(); } 

Note: the error was from my fragment class, and by overriding the onDestroy method as follows, I could fix it.

+1
Oct 21 '18 at 7:34
source share
 if(tv!= null){ ((ViewGroup)tv.getParent()).removeView(tv); // <- fix } 
+1
Dec 28 '18 at 12:13
source share

My mistake was to define the view as follows:

 view = inflater.inflate(R.layout.qr_fragment, container); 

Not enough

 view = inflater.inflate(R.layout.qr_fragment, container, false); 
+1
Mar 13 '19 at 13:05
source share

You can use this method to check if the view has children or not.

 public static boolean hasChildren(ViewGroup viewGroup) { return viewGroup.getChildCount() > 0; } 
0
May 23 '18 at 13:36
source share

check if you have already added a view

 if (textView.getParent() == null) layout.addView(textView); 
0
Sep 08 '18 at 5:44
source share

My case was different, the child view already had a parent view, I add the child view in the parent view to another parent. code example below

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/lineGap" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black1" android:layout_gravity="center" android:gravity="center" /> </LinearLayout> 

And I fanned that view and added LinearLayout to another, then I removed LinaarLayout from the above layout and it started working

The code below fixed the problem:

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/black1" /> 
0
Feb 15 '19 at 7:58
source share

In my case, this happens when I want to add a view by parent to another view

 View root = inflater.inflate(R.layout.single, null); LinearLayout lyt = root.findViewById(R.id.lytRoot); lytAll.addView(lyt); 

You should add a parent view like this

 View root = inflater.inflate(R.layout.single, null); LinearLayout lyt = root.findViewById(R.id.lytRoot); lytAll.addView(root); 
0
May 16 '19 at 9:16
source share



All Articles