The specified child already has a parent. You must call removeView() on the child's parent first (Android)
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
java.lang.IllegalStateException:The specified child already has a parent. You must call removeView() on the child's parent first
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_create_task, container, true)
Change the last true
to false
here. You should not be adding the inflated view to the container here; let the fragment manager do it for you.
java.lang.IllegalStateException: The specified child already has a parent
When you override OnCreateView
in your RouteSearchFragment
class, do you have the
if(view != null) {
return view;
}
code segment?
If so, removing the return statement should solve your problem.
You can keep the code and return the view if you don't want to regenerate view data, and onDestroyView() method you remove this view from its parent like so:
@Override
public void onDestroyView() {
super.onDestroyView();
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeAllViews();
}
}
}
Error The specified child already has a parent after LinearLayout.addView() and then Linearlayout.removeView()
Try to remove the child from its parent before adding it as a subview:
try {
if (linearLayout.getParent() != null) {
// Remove child from parent
((ViewGroup) linearLayout.getParent()).removeView(linearLayout)
}
linear_parent.addView(linearLayout, indexTarget - 1);
}
catch(IllegalStateException e) {
e.printStackTrace();
}
However, consider switching to a RecyclerView
.
It would simplify your code structure.
Related Topics
Oniabpurchasefinished Never Called
Recompile with -Xlint in Android Studio
Add 'Tools:Replace="Android:Value"' to <Meta-Data> Element at Androidmanifest
Am I Getting the Steps Right for Verifying a User's Android In-App Subscription
Make New Activity Appear Behind Old One During Transition
Android Webview: Download Files Like Browsers Do
How to Send Hashmap Value to Another Activity Using an Intent
How to Stop Scrolling in a Gallery Widget
Making a Linearlayout Act Like an Button
Otp (Token) Should Be Automatically Read from the Message
Difference Between iPhone Simulator and Android Emulator
Android: Autocomplete Textview Similar to the Facebook App
Android Drag/Animation of Views
Okhttp Response Callbacks on the Main Thread
How to Use Sms Content Provider? Where Are the Docs
"No Enclosing Instance of Type" Error While Calling Method from Another Class in Android