Nesting fragments inside a viewpager fragment thro

2019-08-14 19:00发布

问题:

I am using a viewPager which contains 2 fragments. The first fragment contains nested fragments and the second one is an empty fragment. Inside the first fragment I am nesting 4 fragments using the child fragment manager. My app crashes with an IndexOutOfBoundsException at runtime.

Here is my code for the first fragment's onCreateView method

 @Override
       public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
 { 
     FragmentManager fragmentManager = getChildFragmentManager();
            fragmentManager.beginTransaction()
                    .add(R.id.frag1_container, frag1)
                    .commit();

            fragmentManager.beginTransaction()
                    .add(R.id.frag2_container, frag2)
                    .commit();

            fragmentManager.beginTransaction()
                    .add(R.id.frag3_container, frag3)
                    .commit();

            fragmentManager.beginTransaction()
                    .add(R.id.frag4_container, frag4)
                    .commit();
        return super.onCreateView(inflater, container, savedInstanceState);
  }

Here is the stack trace for the exception:

 D/AndroidRuntime: Shutting down VM
 E/AndroidRuntime: FATAL EXCEPTION: main
 E/AndroidRuntime: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2
 E/AndroidRuntime:     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
 E/AndroidRuntime:     at java.util.ArrayList.set(ArrayList.java:481)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.makeInactive(FragmentManager.java:1192)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1099)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1235)
 E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:710)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
 E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5292)
 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

回答1:

Figured out what was causing the error. Inside one of my fragments,I was calling

 getActivity().getSupportFragmentManager().beginTransaction().remove(instance).commit();

which was using the fragment manager of the Activity instead of child fragment manager of the fragments'.

and hence this error:

java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2

where 3 was the index of the nested fragment and 2 was the size returned by the Activity's fragment manager.

Fixed it by using the fragment manager from the fragment instead of the Activity:

ThisFragment.this.getFragmentManager().beginTransaction().remove(instance).commit();