0
votes

i keep getting following errors, it's basically same error but keeps occuring on different lines

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:3883)

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4312)

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4310)

Things i have tried :-

  • while adding any fragment i add this code in onCreateView

    ((ViewGroup) view.getParent()).removeView(view); view = inflater.inflate(R.layout.create_carpool_layout, container, false); return view;

    and then inflate the view

  • While inflating layouts / views in adapter also i make sure the view is removed from parent view

I am not sure what is causing this problem

1

1 Answers

0
votes

Try double checking how you are inflating the view.

There are two possible ways:

  1. Passing the parent to the inflater:

    LayoutInflater.from(context).inflate(R.layout.layout, this, true);
    
  2. Keep the view without the parent:

    LayoutInflater.from(context).inflate(R.layout.layout, null);
    

When using the first method, you cannot call addView() with the view as parameter.
Using the second way, the view is not pinned to parent, therefore you can invoke addView()