32

Into my project I am using viewpager with three tabs named History,Main,Map.Main activity contain Timer,stopwatch,etc. Map display the google map. and into History right now I am using only simple Textview.

Viewpager flow direction : History - Main - Map

I set the Main as the current item(Bydefault Main tag display).Now when I am swipe from Main > Map and Map > Main its working perfect. but when swipe from Main > History there is no error but back return from History to Main( History > Main) eclipse give me error like :

E/AndroidRuntime(  533): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

And FYI I am using intent into Map Activity.Please check below code.Please let me know how can solve my problem.

ViewPager class:

@Override
    public Fragment getItem(int position) {
        // return SwipeyTabFragment.newInstance(TITLES[position]);

        Fragment f = new Fragment();

        switch (position) {
        case 0:
            f = History.newInstance(position);
            break;
        case 1:
            f = Main.newInstance(position);
            break;
        case 2:
            f = Map.newInstance(position);
            break;

        }

        return f;

    }

History.class:

public class History extends Fragment {

public static Fragment newInstance(int position) {
    History f = new History();
    Bundle args = new Bundle();

    args.putInt("title", position);
    f.setArguments(args);
    return f;

}

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.history, null);


     ( (TextView) root.findViewById(R.id.text)).setText("Hello");
    return root;
} 

}

Main.class :

 public class Main extends Fragment implements GPSCallback {

 ......

public static Fragment newInstance(int position) {
    Main f = new Main();
    Bundle args = new Bundle();

    args.putInt("title", position);
    f.setArguments(args);
    return f;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final ViewGroup root = (ViewGroup) inflater
            .inflate(R.layout.main, null);

    .......

    return root;
 }

    ........
}

Map.class :

public class Map extends Fragment {

private static final String KEY_STATE_BUNDLE = "localActivityManagerState";

private LocalActivityManager mLocalActivityManager;

protected LocalActivityManager getLocalActivityManager() {
    return mLocalActivityManager;
}

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

    Bundle state = null;
    if (savedInstanceState != null) {
        state = savedInstanceState.getBundle(KEY_STATE_BUNDLE);
    }

    mLocalActivityManager = new LocalActivityManager(getActivity(), true);
    mLocalActivityManager.dispatchCreate(state);
}



public static Fragment newInstance(int position) {
    Map f = new Map();
    Bundle args = new Bundle();

    args.putInt("title", position);
    f.setArguments(args);
    return f;

}


  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        Intent i = new Intent(getActivity(), MapViewActivity.class);
       // Intent i = new Intent(getActivity(), hellogooglemap.class);
        Window w = mLocalActivityManager.startActivity("tag", i); 

        View currentView=w.getDecorView(); 
        currentView.setVisibility(View.VISIBLE); 
        currentView.setFocusableInTouchMode(true); 
        ((ViewGroup) currentView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        return currentView;


      /*ViewGroup root = (ViewGroup) inflater.inflate(R.layout.history, null);


         ( (TextView) root.findViewById(R.id.text)).setText("Hello");
        return root;*/

}


 @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(KEY_STATE_BUNDLE,
                mLocalActivityManager.saveInstanceState());
    }

    @Override
    public void onResume() {
        super.onResume();
        mLocalActivityManager.dispatchResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mLocalActivityManager.dispatchPause(getActivity().isFinishing());
    }

    @Override
    public void onStop() {
        super.onStop();
        mLocalActivityManager.dispatchStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocalActivityManager.dispatchDestroy(getActivity().isFinishing());
    }

}

Logcat :

 E/AndroidRuntime(  533): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 E/AndroidRuntime(  533):   at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.addView(ViewGroup.java:1871)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.addView(ViewGroup.java:1828)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.addView(ViewGroup.java:1808)
 E/AndroidRuntime(  533):   at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
 E/AndroidRuntime(  533):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:874)
 E/AndroidRuntime(  533):   at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1240)
 E/AndroidRuntime(  533):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:612)
 E/AndroidRuntime(  533):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
 E/AndroidRuntime(  533):   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
 E/AndroidRuntime(  533):   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
 E/AndroidRuntime(  533):   at android.support.v4.view.ViewPager.populate(ViewPager.java:804)
 E/AndroidRuntime(  533):   at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
 E/AndroidRuntime(  533):   at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
 E/AndroidRuntime(  533):   at android.view.View.draw(View.java:6883)
 E/AndroidRuntime(  533):   at android.widget.FrameLayout.draw(FrameLayout.java:357)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
 E/AndroidRuntime(  533):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
 E/AndroidRuntime(  533):   at android.view.View.draw(View.java:6883)
 E/AndroidRuntime(  533):   at android.widget.FrameLayout.draw(FrameLayout.java:357)
 E/AndroidRuntime(  533):   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
 E/AndroidRuntime(  533):   at android.view.ViewRoot.draw(ViewRoot.java:1522)
 E/AndroidRuntime(  533):   at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
 E/AndroidRuntime(  533):   at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
 E/AndroidRuntime(  533):   at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(  533):   at android.os.Looper.loop(Looper.java:130)
 E/AndroidRuntime(  533):   at android.app.ActivityThread.main(ActivityThread.java:3683)
 E/AndroidRuntime(  533):   at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(  533):   at java.lang.reflect.Method.invoke(Method.java:507)
 E/AndroidRuntime(  533):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 E/AndroidRuntime(  533):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 E/AndroidRuntime(  533):   at dalvik.system.NativeStart.main(Native Method)
 W/ActivityManager(   68):   Force finishing activity      com.android.gps/.viewpager.SwipeyTabsSampleActivity
 W/ActivityManager(   68): Activity pause timeout for HistoryRecord{40716740 com.android.gps/.viewpager.SwipeyTabsSampleActivity}

Thanks.

EDIT:

Without intent into Map activity it works pwrfect.I mean when i am using same as history into map there is no any error.

9 Answers9

41

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);
mc.dev
  • 2,675
  • 3
  • 21
  • 27
  • Given the description from the documentation, this makes perfect sense, and was what fixed my problem. – CoatedMoose Jan 31 '13 at 23:14
  • 1
    for me :) View res = inflater.inflate(R.layout.fragment_guide_search, null); – cV2 Feb 15 '14 at 10:59
  • for me, the second option too – androidevil Apr 03 '14 at 13:51
  • 1
    Note that the second option will ignore layout params on the root inflated view; if you have the container view available the first option (container, false) is almost always the correct option. – Karu May 26 '14 at 05:22
27

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.

tim687
  • 2,256
  • 2
  • 17
  • 28
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46
  • This does not explain why the crash is happening. It often happens, because the wrong view is returned in the Fragments `onCreateView`. In this question, most likely the onCreateView of Map.java is the problem. – Aura Lee Sep 16 '21 at 13:28
4

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;
}
vbence
  • 20,084
  • 9
  • 69
  • 118
Lobsterman
  • 310
  • 2
  • 11
2

// 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);
Richa
  • 700
  • 8
  • 12
  • This is the correct solution if you want to do something after onCreate() method. You dont need adapter.setOffScreenPageLimit(int), Thanks Richa – Khalid Lakhani Apr 22 '20 at 14:21
2

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());

Kavya Shravan
  • 333
  • 3
  • 6
  • This was it for me when everything else failed. Also to anyone reading, make sure to check if the `getActivity()` is not null because it can happen during device rotation or activity restart (maybe user changed app theme for example) – Shadow Oct 28 '20 at 15:19
1

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;
  }
android developer
  • 114,585
  • 152
  • 739
  • 1,270
1
@Override
public Object instantiateItem(View arg0, int arg1) {
    Log.d("instantiateItem", ""+arg0+" "+arg1);
    try { 
        if(mListViews.get(arg1).getParent()==null)
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);  
        else{
            // I am new to android, it is strange that the view to be added is already bound to a parent
            // Through trials and error I solve this problem with the following codes
            // Add that the element of mlistviews is listview in pagerview;
            ((ViewGroup)mListViews.get(arg1).getParent()).removeView(mListViews.get(arg1));

            ((ViewPager) arg0).addView(mListViews.get(arg1), 0); 
        }
    } catch (Exception e) {   
        Log.d("parent=", ""+mListViews.get(arg1).getParent()); 
        e.printStackTrace();  
    }  
    return mListViews.get(arg1);
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
0

if you are working with FragmentPagerAdapter then just use destroyItem method inside FragmentPagerAdapter.

@Override
public void destroyItem(ViewGroup container, int position, Object object) 
{
        FragmentTransaction ft=fm.beginTransaction();
        ft.remove((Fragment) object);
        ft.commit();
}

setOffscreenPageLimit(int limit) consumes more memory and in some cases its not works perfectly. its retain state in either side see description.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.

Pranav
  • 4,172
  • 3
  • 30
  • 31
-1

Check if you have any ConstrainLayout (or similar) in your layouts files. As may be the reason for throwing that error.

Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23