Java.Lang.Illegalstateexception: the Specified Child Already Has a Parent

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



Leave a reply



Submit