当Fragment和ViewPager一起交错使用时,出现以下异常:
Recursive entry to executePendingTransactions
- 03-26 16:29:57.149: E/AndroidRuntime(2644): FATAL EXCEPTION: main
- 03-26 16:29:57.149: E/AndroidRuntime(2644): java.lang.IllegalStateException: Recursive entry to executePendingTransactions
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:522)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:494)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1220)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.view.View.dispatchRestoreInstanceState(View.java:11910)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2584)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2590)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2590)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.view.View.restoreHierarchyState(View.java:11888)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.Fragment.restoreViewState(Fragment.java:417)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.os.Handler.handleCallback(Handler.java:615)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.os.Handler.dispatchMessage(Handler.java:92)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.os.Looper.loop(Looper.java:137)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at android.app.ActivityThread.main(ActivityThread.java:4745)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at java.lang.reflect.Method.invokeNative(Native Method)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at java.lang.reflect.Method.invoke(Method.java:511)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
- 03-26 16:29:57.149: E/AndroidRuntime(2644): at dalvik.system.NativeStart.main(Native Method)
然后上网搜了下,有以下解决方法:
错误描述:ViewPager: Recursive entry to executePendingTransactions
错误产生:
在Fragment里面添加Viewpager,Viewpager由多个Fragment组成,在viewpager的setAdapter时产生错误
解决方案:
It can be done, but has be done through an Asysnc or a handler as you have to let the first fragments transaction complete first. You can have fragments within fragments.
实例:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
adapter = new MainFragmentPagerAdapter(getFragmentManager(), fragments);
viewPager.setAdapter(adapter);
switchPager(0);
}
}, 100);
}
但是试了下,发现效果并不理想,这样延时了viewPager的加载,导致滑动到某页面时,页面为空,需等待一段时间后再滑动回来才能正常显示,于是上StackOverFlow上看看,找到了最优解决方案:
New version of Support Library v4 (or Android 4.2, ofcourse) resolve this problem much simply. For do this, simply do constructor of your custom FragmentPagerAdapter like this:
- public CustomFragmentPagerAdapter(android.support.v4.app.Fragment fragment)
- {
- super(fragment.getChildFragmentManager());
- // write your code here
- }
于我而言,之前在构造方法里,super()参数是activity.getSupportFragmentManager(),如上,将其改为
getChildFragmentManager()即可。
需要注意的是:需要将v4包更新到最新版本,我的v4包以更新到版本12,已经解决上述问题!
希望能帮助到遇到此问题的朋友,对了,StackOverFlow上原文地址,有兴趣的朋友可以看看: