Android Switching between Viewpager produces Android crash

ぃ、小莉子 提交于 2020-01-25 05:55:52

问题


I have five fragments in an View pager. One of them is a BarcodeFragment BarcodeFragment and I am getting a very weird android crash when I swipe between ViewPager fragments quickly. The crash Im getting is on the android side. This is the log.

java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.set(ArrayList.java:481)
            at android.support.v4.app.FragmentManagerImpl.makeInactive(FragmentManager.java:1169)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1078)
            at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1212)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:652)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

I suspect its happening because of the following : Whenever I am at the barcodeFragment and I want to switch to another fragment in the viewpager I remove the fragment using the following code:

if (getChildFragmentManager().findFragmentById(R.id.barcode_Fragment) != null) {
                if (mBarcodeFragment != null && mBarcodeFragment.isResumed()) {
                    mBarcodeFragment.onDestroy();
                    mFragmentManager.beginTransaction().remove(mBarcodeFragment).commitAllowingStateLoss();
                    mBarcodeFragment = null;
                }
            }

And whenever I am coming back to the BaseScanFragment (Which is the fragment which calls the Barcode Fragment) , I am adding the fragment again through the following code.:

mBarcodeFragment = new BarcodeFragment();
    getChildFragmentManager().beginTransaction().replace(R.id.barcode_Fragment, mBarcodeFragment).commit();
    mBarcodeFragment.setDecodeFor(IScanResultHandler.MODE.SHOP_NOW_MODE);
    mBarcodeFragment.setScanResultHandler((IScanResultHandler) getSherlockActivity());

Can anyone guess what the problem is?


回答1:


I have faced this problem earlier when I was working with nested fragments. In my case it was occurring because I was using the wrong Fragment manager i.e I was accidentally using the Activity fragment manager for child fragment transactions

java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7

The index points to the fragment in which this error is occurring i.e 7.



来源:https://stackoverflow.com/questions/26041795/android-switching-between-viewpager-produces-android-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!