使用ViewPager和FragmentStatePagerAdapter时出现IllegalStateException问题

时间:2014-10-17 07:35:39

标签: android android-fragments android-viewpager android-fragmentactivity

使用带有FragmentStatePagerAdapter的ViewPager时出现问题。基本上,在运行时,我想随着时间的推移向ViewPager添加新的片段。由于某种原因,第一个片段成功添加,但第二个片段导致“IllegalStateException”。

       public class AdActivity extends FragmentActivity implements IChannelListener 
{
   private static final String TAG = "AdActivity";
   public static int NUM_PAGES = 0;
   private VerticalViewPager mPager;
   private PagerAdapter mPagerAdapter;

   public Channel getChannel() {
      return channel;
   }

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      AdController.getInstance().setAdActivity(this);
      Log.d(TAG, "onCreate()");
      setContentView(R.layout.activity_ad);

      // Instantiate a ViewPager and a PagerAdapter.
      mPager = (VerticalViewPager) findViewById(R.id.pager);
      mPagerAdapter = new AdPagerAdapter(getFragmentManager());
      mPager.setAdapter(mPagerAdapter);
      mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
          @Override
          public void onPageSelected(int position) {
              invalidateOptionsMenu();
          }
      });

      // To stop destroying unused fragments
      mPager.setOffscreenPageLimit(15);

      Log.d(TAG, "endOfOnCreate()");
   }

   public void insertAdFragment(final Ad ad)   {
       Log.d(TAG, "insertAdFragment()");
       runOnUiThread(new Runnable(){
            @Override
            public void run() 
            {
                AdFragment fragment = new AdFragment(ad);
                AdFragment.fragments.add(0, fragment);
                NUM_PAGES = NUM_PAGES + 1;
                mPagerAdapter.notifyDataSetChanged();
            }
       });

       Log.d(TAG, "end insertAdFragment()");
   }

   /*
    * Pager Adapter that represents the objects in sequence.
    * */
   private class AdPagerAdapter extends FragmentStatePagerAdapter 
   {
    private static final String TAG = "AdPagerAdapter";

       public AdPagerAdapter(FragmentManager fragmentManager) 
       {
           super(fragmentManager);
           Log.d(TAG, "AdPagerAdapter()");
       }

    @Override
       public Fragment getItem(int position) 
       {
        Log.d(TAG, "getItem(" + position + ") : size of fragments is... " + AdFragment.fragments.size());
           //return AdFragment.create(position);
        return AdFragment.fragments.get(position);
       }

       @Override
       public int getCount() {
           Log.d(TAG, "getCount() : " + NUM_PAGES);
           return NUM_PAGES;
       }
   }
}

每分钟调用一次 insertAdFragment 上面的函数。在到达函数之前,NUM_PAGES的值为0.将片段添加到“片段”列表的开头。然后,调用notifyDataSetChanged()。以下是AdPagerAdapter:

private class AdPagerAdapter extends FragmentStatePagerAdapter 
   {
    private static final String TAG = "AdPagerAdapter";

       public AdPagerAdapter(FragmentManager fragmentManager) 
       {
           super(fragmentManager);
           Log.d(TAG, "AdPagerAdapter()");
       }

    @Override
       public Fragment getItem(int position) 
       {
        Log.d(TAG, "getItem(" + position + ") : size of fragments is... " + AdFragment.fragments.size());

        return AdFragment.fragments.get(position);
       }

       @Override
       public int getCount() {
           Log.d(TAG, "getCount() : " + NUM_PAGES);
           return NUM_PAGES;
       }
   }

当添加第二个片段时,App崩溃了:(呃我不能为我的生活弄明白这一点。

2 个答案:

答案 0 :(得分:1)

将您的Fragment类添加到mainactivity中的List,如下所示

private void ViewPagerInitialize()
     {
         List<Fragment> fragments = new Vector<Fragment>();
            fragments.add(Fragment.instantiate(this, AndroidFragment.class.getName()));
            fragments.add(Fragment.instantiate(this, AppleFragment.class.getName()));
            fragments.add(Fragment.instantiate(this, MicrosoftFragment.class.getName()));
            mPageradapter  = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
            mViewPager = (ViewPager)super.findViewById(R.id.pager);
            mViewPager.setAdapter(mPageradapter);
            mViewPager.setOnPageChangeListener(this);
     }

并使用此视图寻呼机适配器类

public class MyPagerAdapter extends FragmentPagerAdapter
{
    private List<Fragment> fragments;
    public MyPagerAdapter(FragmentManager mFManager,List<Fragment> fragments) {
        super(mFManager);
        this.fragments = fragments;
    }
    @Override
    public Fragment getItem(int arg0) {

        return fragments.get(arg0);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

答案 1 :(得分:1)

只需实现onpagechangelistener并覆盖此方法

@Override
    public void onPageScrollStateChanged(int arg0) 
    {
    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) 
    {
    }
    @Override
    public void onPageSelected(int arg0) {
        mTabHost.setCurrentTab(arg0);
    }