java.lang.IllegalStateException: Recursive entry to executePendingTransactions

在Fragment里面添加Viewpager,Viewpager由多个Fragment组成

java.lang.IllegalStateException: Recursive entry to executePendingTransactions
找到发送错误的地方是viewpager.setAdapter()处。

我的应用场景是: 嵌套使用Fragment使用,最外层使用了一个第三方的库,里面使用FragmentActivity,每个Fragment中又使用ViewPager再嵌套Fragment

解决方法:嵌套Fragment设置PageAdapter时要使用getChildFragmentManager。

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
}

This work because new Android version approve using nested Fragments

我这里没有修改CustomFragmentPagerAdapter,而是新建CustomFragmentPagerAdapter对象时使用参数改为this.getChildFragmentManager();

adapter = new TabPageIndicatorAdapter(this.getChildFragmentManager()); 
TabPageIndicatorAdapter类内容不变:
 /**
	 * ViewPager
	 * @author liwenjin
	 *
	 */
    class TabPageIndicatorAdapter extends FragmentPagerAdapter {
       
    	public TabPageIndicatorAdapter(FragmentManager  fm) {
            super(fm);
            
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new ItemFragment();  
            Bundle args = new Bundle();  
            args.putString("arg", TITLE[position]);  
            fragment.setArguments(args);  
            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position % TITLE.length];
        }

        @Override
        public int getCount() {
            return TITLE.length;
        }
    }


 
   
 
   

 
   
 
   在查找资料是看到网上,这里先记下解决方案:
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);







 
   
 
  


你可能感兴趣的:(遇到的问题)