Java.lang.IllegalStateException: The specified child already has a parent

I use fragments when I create a fragment for the first time. but the second time I got this exception. I could not find the line where I got the error?

04-04 08:51:54.320: E/AndroidRuntime(29713): FATAL EXCEPTION: main 04-04 08:51:54.320: E/AndroidRuntime(29713): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child parent first. 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addViewInner(ViewGroup.java:3013) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2902) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2859) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2839) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.NoSaveStateFrameLayout.wrap(Unknown Source) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.BackStackRecord.run(Unknown Source) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Handler.handleCallback(Handler.java:587) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Handler.dispatchMessage(Handler.java:92) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Looper.loop(Looper.java:132) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.app.ActivityThread.main(ActivityThread.java:4126) 04-04 08:51:54.320: E/AndroidRuntime(29713): at java.lang.reflect.Method.invokeNative(Native Method) 04-04 08:51:54.320: E/AndroidRuntime(29713): at java.lang.reflect.Method.invoke(Method.java:491) 04-04 08:51:54.320: E/AndroidRuntime(29713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 04-04 08:51:54.320: E/AndroidRuntime(29713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 04-04 08:51:54.320: E/AndroidRuntime(29713): at dalvik.system.NativeStart.main(Native Method) 

This is what I do when I click on an element of my list fragment.

 // If we are not currently showing a fragment for the new // position, we need to create and install a new one. RouteSearchFragment df = RouteSearchFragment.newInstance(index); // Execute a transaction, replacing any existing fragment // with this one inside the frame. FragmentTransaction ft = fragmentManager.beginTransaction(); ft.replace(R.id.details_full, df); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); 

The first time this is normal, I click element2 from the list, this is also normal; but when I go back to element1, I got this error.

Thanks to everyone!

+81
android android-layout android-fragments
Apr 04 '12 at 8:00
source share
9 answers

When you override OnCreateView in your RouteSearchFragment class, you have

 if(view != null) { return view; } 

code segment?

If so, deleting the return statement should solve your problem.

You can save the code and return the view if you do not want to regenerate the view data, and onDestroyView () method you remove this view from your parent, for example:

  @Override public void onDestroyView() { super.onDestroyView(); if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) { parent.removeAllViews(); } } } 
+24
Apr 04 2018-12-12T00:
source share

Sorry to post an old question, but I was able to fix it using a completely different solution. I was getting this exception, but I changed the first line of my override onCreatView:

 View result = inflater.inflate(R.layout.customer_layout, container); 

... to that:

 View result = inflater.inflate(R.layout.customer_layout, container, false); 

I have no idea why, but using an override that takes a boolean, as the third parameter fixed it. I think he tells Fragment and / or Act not to use the โ€œcontainerโ€ as the parent of the newly created view. NTN!

+341
Oct 19
source share

I have come across this problem many times. Add the following code to solve this problem:

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

thank

+39
Apr 15 '13 at 11:54 on
source share

If you have this statement ..

 View view = inflater.inflate(R.layout.fragment1, container);//may be Incorrect 

Then try this. Add false as the third argument. Maybe this can help.

 View view = inflater.inflate(R.layout.fragment1, container, false);//correct one 
+28
Jul 18 '14 at 7:52
source share

I had this code in a fragment and it crashes if I try to return to this fragment

 if (mRootView == null) { mRootView = inflater.inflate(R.layout.fragment_main, container, false); } 

after collecting responses to this thread, I realized that the mRootView parent still has mRootView as a child. So that was my fix.

 if (mRootView == null) { mRootView = inflater.inflate(R.layout.fragment_main, container, false); } else { ((ViewGroup) mRootView.getParent()).removeView(mRootView); } 

hope this helps

+19
Sep 04 '13 at 11:29 on
source share

This also happens when the view returned by onCreateView () is not a bloated view.

Example:

 View rootView = inflater.inflate(R.layout.my_fragment, container, false); TextView textView = (TextView) rootView.findViewById(R.id.text_view); textView.setText("Some text."); return textView; 



Fix:

 return rootView; 

Instead:

 return textView; // or whatever you returned 
+13
Dec 19 '14 at 20:37
source share

I had this problem and I could not solve it in Java code. The problem was in my xml.

I tried to add a textView to the container, but wrapped the textView inside a LinearLayout.

This was the original xml file:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:paddingLeft="16dp" android:paddingRight="16dp" android:textColor="#fff" android:background="?android:attr/activatedBackgroundIndicator" android:minHeight="?android:attr/listPreferredItemHeightSmall"/> </LinearLayout> 

Now with the removal of LinearLayout:

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:paddingLeft="16dp" android:paddingRight="16dp" android:textColor="#fff" android:background="?android:attr/activatedBackgroundIndicator" android:minHeight="?android:attr/listPreferredItemHeightSmall"/> 

It didnโ€™t seem to me, but it was a trick, and I did not change my Java code at all. This is all in xml.

+4
Oct 13 '15 at 2:00
source share

You add a View to the layout , but the view is already in a different layout . A view cannot be in several places.

+1
Apr 04 '12 at 8:21
source share

This solution may help:

 public class FragmentItem extends Android.Support.V4.App.Fragment { View rootView; TextView textView; public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (rootView != null) { ViewGroup parent = (ViewGroup)rootView.Parent; parent.RemoveView(rootView); } else { rootView = inflater.Inflate(Resource.Layout.FragmentItem, container, false); textView = rootView.FindViewById<TextView>(Resource.Id.textViewDisplay); } return rootView; } } 
0
Jun 18. '14 at 6:32
source share



All Articles