Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve for viewpager : The specified child already has a parent. You must call removeView() on the child's parent first

I had the same problem when I used

View res = inflater.inflate(R.layout.fragment_guide_search, container);

inside Fragment.onCreateView(...

You must call

View res = inflater.inflate(R.layout.fragment_guide_search, container, false);

or

View res = inflater.inflate(R.layout.fragment_guide_search, null);

I have also faced this problem.

You can solve it by just add single line mViewPager.setOffscreenPageLimit(3);

public class SwipeyTabsSampleActivity extends FragmentActivity {

...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    mTabs = (SwipeyTabs) findViewById(R.id.swipeytabs);

    SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this,
            getSupportFragmentManager());
    mViewPager.setAdapter(adapter);

    mViewPager.setOffscreenPageLimit(3);  <------  Add this one
}

}

Good Luck.


First, the answer by RB Patel to insert code (shown below), worked like a charm, but as I read about this feature, I realized it was intended as a potential optimization and isn't actually the solution to the bug.

mViewPager.setOffscreenPageLimit(3);

The Android Developer site says the following about this method:

setOffscreenPageLimit(int limit) - Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

It seems like this just sets a limit on the number of pages cached to the right or left of the current page. In my case this was just a band aid for the problem, yes the error stopped and things worked smoothly, but I still had bad code lurking in my program which simply not being executed. I believe this method was intended to be used as an optimization and not an actual fix to the bug.

I ran into this problem when trying to overload the instantiateItem() method in a custom class which extends PagerAdapter. My error was thrown after I would navigate back to a tab/page which I had previously visited/instantiated.

I am new to Android but I'll do my best to explain my solution. The line which throw this error was the following:

v.addView(child);

If I understand this correctly, it was choking because the child was already instantiated previously and assigned a parent.

When I would navigate back to a previously visited view/page I was trying to add a view which already had a parent layout view to a new Layout, hence the error.

Solution: I just checked to see if the child view I was trying to display (instantiate) was already instantiated somewhere else by seeing if it had a parent view set. If the child view had already been created, then I just used the parent of the child view instead of creating a new Layout.

Here is my code (I apologize if it offends any experts):

@Override
public Object instantiateItem(View container, int position) { 
    LinearLayout v = new LinearLayout(mContext);

    //the view being loaded (unless it already was then we have to handle it)
    View child = mViews.get(position);

    //If the child view was loaded already then it will have a parent view it belongs to
    //We return the parent view instead of adding the child to the LinearLayout we just created and returning that new layout.
    if(child.getParent() != null) {
        View parent = (View) child.getParent();
        ((ViewPager) container).addView(parent);
        return parent;
    }

    //first time instantiating a child view/page
    v.addView(child);            

    // These lines execute the first time a given page is instantiated
    ((ViewPager) container).addView(v);

    return v;
}

// if the child view to be added is already bound to a parent, then remove the child from the parent and then add again to the container.

if (v.getParent() != null) {
  ((ViewGroup) v.getParent()).removeView(v);
}
container.addView(v);

For a adapter if you are using with tabLayout or viewPager, instead of getChildFragmentManager() replace it with getActivity().getSupportFragmentManager() like below.

TabAdapter adapter = new TabAdapter(getActivity(), getActivity().getSupportFragmentManager());


Here's my solution (used in each of the fragments) , which allows both smoothness and avoids memory problems:

...
private LayoutInflater mInflater;
private WeakReference<View> mRootView = null;
...

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) 
  {
  if (inflater != null)
    mInflater = inflater;
  else
    mInflater = LayoutInflater.from(getActivity());
  View rootView = mRootView == null ? null : mRootView.get();
  if (rootView != null) 
      {
      final ViewParent parent = rootView.getParent();
      if (parent != null && parent instanceof ViewGroup)
        ((ViewGroup) parent).removeView(rootView);  
      }
  else 
       {
       rootView = mInflater.inflate(R.layout.fragment_test, null, false);
       mRootView = new WeakReference<View>(rootView);
       }
  return rootView;
  }