1

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)
Venkata Narayana
  • 1,657
  • 12
  • 25
  • What does your ViewPager's Adapter return for `getCount()`? – Mike M. Nov 26 '15 at 23:57
  • it was returning 2 (which was correct).Found out the cause, posted it below.The part of the code which was causing the error was not mentioned in the question because the stack trace was not clearly showing where it was coming from(and i couldn't figure it out then that it would be relevant). – Venkata Narayana Nov 27 '15 at 08:54

1 Answers1

2

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();
Venkata Narayana
  • 1,657
  • 12
  • 25