Recursive entry to executePendingTransactions

当Fragment和ViewPager一起交错使用时,出现以下异常:

Recursive entry to executePendingTransactions

[java]  view plain copy
  1. 03-26 16:29:57.149: E/AndroidRuntime(2644): FATAL EXCEPTION: main  
  2. 03-26 16:29:57.149: E/AndroidRuntime(2644): java.lang.IllegalStateException: Recursive entry to executePendingTransactions  
  3. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)  
  4. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)  
  5. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)  
  6. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)  
  7. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:522)  
  8. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:494)  
  9. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1220)  
  10. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.view.View.dispatchRestoreInstanceState(View.java:11910)  
  11. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2584)  
  12. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2590)  
  13. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2590)  
  14. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.view.View.restoreHierarchyState(View.java:11888)  
  15. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.Fragment.restoreViewState(Fragment.java:417)  
  16. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)  
  17. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)  
  18. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)  
  19. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)  
  20. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)  
  21. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.os.Handler.handleCallback(Handler.java:615)  
  22. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.os.Handler.dispatchMessage(Handler.java:92)  
  23. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.os.Looper.loop(Looper.java:137)  
  24. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at android.app.ActivityThread.main(ActivityThread.java:4745)  
  25. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at java.lang.reflect.Method.invokeNative(Native Method)  
  26. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at java.lang.reflect.Method.invoke(Method.java:511)  
  27. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)  
  28. 03-26 16:29:57.149: E/AndroidRuntime(2644):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  
  29. 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:
[java]  view plain copy
  1. public CustomFragmentPagerAdapter(android.support.v4.app.Fragment fragment)  
  2. {  
  3.     super(fragment.getChildFragmentManager());  
  4.   
  5.     // write your code here  
  6. }  
This work because new Android version approve using nested Fragments.

于我而言,之前在构造方法里,super()参数是activity.getSupportFragmentManager(),如上,将其改为
getChildFragmentManager()即可。

需要注意的是:需要将v4包更新到最新版本,我的v4包以更新到版本12,已经解决上述问题!
希望能帮助到遇到此问题的朋友,对了,StackOverFlow上原文地址,有兴趣的朋友可以看看:
http://stackoverflow.com/questions/7338823/viewpager-recursive-entry-to-executependingtransactions

你可能感兴趣的:(android,异常情况)