在viewpager片段中嵌套片段会引发IndexOutOfBoundsException [英] Nesting fragments inside a viewpager fragment throws IndexOutOfBoundsException
问题描述
我正在使用包含2个片段的viewPager。
第一个片段包含嵌套片段,第二个片段为空片段。
在第一个片段内,我使用子片段管理器嵌套4个片段。
我的应用在运行时因 IndexOutOfBoundsException 而崩溃。
这是我第一个片段的 onCreateView 的代码>方法
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState)
{
FragmentManagerfragmentManager = 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);
}
以下是异常的堆栈跟踪:
D / AndroidRuntime:关闭VM
E / AndroidRuntime:致命例外:main
E / AndroidRuntime:java.lang.IndexOutOfBoundsException:无效索引3,大小为2
E / AndroidRuntime:在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
E / AndroidRuntime:在java.util.ArrayList.set(ArrayList.java :481)
E / AndroidRuntime:在android.support.v4.app.FragmentManagerImpl.makeInactive(FragmentManager.java:1192)
E / AndroidRuntime:在android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1099)
E / AndroidRuntime:在android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1235)
E / AndroidRuntime:在android.support.v4.app .BackStackRecord.run(BackStackRecord.java:710)
E / AndroidRuntime:at android.support.v4.app.FragmentManagerImpl.execPendingActions(Frag mentManager.java:1501)
E / AndroidRuntime:在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:458)
E / AndroidRuntime:在android.os.Handler.handleCallback (Handler.java:739)
E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:95)
E / AndroidRuntime:在android.os.Looper.loop(Looper.java :135)
E / AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5292)
E / AndroidRuntime:在java.lang.reflect.Method.invoke(本机方法)
E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java:372)
E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java: 904)
E / AndroidRuntime:com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
找出导致错误的原因。
在我的一个片段中,我叫
getActivity()。getSupportFragmentManager()。beginTransaction()。remove (instance).commit();
使用的是Activity的片段管理器,而不是片段的子片段管理器。 p>
,因此出现此错误:
java.lang.IndexOutOfBoundsException:无效的索引3,大小为2
其中3是嵌套片段的索引,2是返回的大小
使用片段中的片段管理器而不是活动来修复它:
ThisFragment.this.getFragmentManager()。beginTransaction()。remove(instance).commit();
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)
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();
这篇关于在viewpager片段中嵌套片段会引发IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!