IllegalStateException: The application's PagerAdapter changed the adapter's content without calling PagerAdapter#notifyDataSetChanged

杀马特。学长 韩版系。学妹 提交于 2019-11-27 01:31:26
Zagorax

I had a hard time making my ViewPager working. At the end, it seems that the example in the documentation is wrong.

The addTab method should be as follows:

public void addTab(Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        notifyDataSetChanged();
        mActionBar.addTab(tab);
    }

Notice the order of the last three operations. In the original example, notifyDataSetChanged was called after the mActionBar.addTab function.

Unfortunately, as soon as you call the addTab on the ActionBar, the first tab you add is automatically selected. Because of this, the onTabSelected event is fired and while trying to retrieve the page, it throws the IllegalStateException because it notices a discrepancy between the expected item count and the actual one.

I fixed it by callinig notifyDataSetChanged() once and just before next call of getCount():

private boolean doNotifyDataSetChangedOnce = false;

@Override
public int getCount() {

  if (doNotifyDataSetChangedOnce) {
    doNotifyDataSetChangedOnce = false;
    notifyDataSetChanged();
  }

  return actionBar.getTabCount();

}

private void addTab(String text) {

  doNotifyDataSetChangedOnce = true;

  Tab tab = actionBar.newTab();
  tab.setText(text);
  tab.setTabListener(this);
  actionBar.addTab(tab);

}

private void removeTab(int position) {

  doNotifyDataSetChangedOnce = true;

  actionBar.removeTabAt(position);

}

I was getting this error like you by referencing the tabs within getCount():

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

When instantiated this should either be passed in as a separate variable, or the pager should be set up after the collection has been populated / mutated.

try this. I worked

protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            System.out.println("Asyncrona onPostExecute");
            /*::::::::::::::::::: Guardo los cambios  ::::::::::::::*/
            //menuTabs = menuTabs2;
            viewPager = (ViewPager) rootView.findViewById(R.id.viewPager_menu);
            costumAdapter = new CostumAdapter2(getActivity().getSupportFragmentManager());
            costumAdapter.notifyDataSetChanged();

            viewPager.setAdapter(costumAdapter);
            tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_menu);
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    System.out.println(" La Seleccionado: " + tab.getText()+", POSICION: "+tab.getPosition());
                    viewPager.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    viewPager.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {
                    viewPager.setCurrentItem(tab.getPosition());
                }

            });
            viewPager.setAdapter(costumAdapter);
            tabLayout.setupWithViewPager(viewPager);
            loading.closeMessage();

        }

My issue wasn't exactly the same, but similar context.

I have an AlertDialog getting shown in a layout that has ViewPager and is using AppCompatActivity. I was getting the IllegalStateException and crash when rotating the screen.

Thanks to the answers from answer @almisoft and @Zagorax, I tried calling notifyDataSetChanged(); right before I show the dialog, and the problem seems to have gone away.

I had the same problem in FragmentStatePager adapter. in onResume() When reInitialize the adpater and ViewPager, the same error IllegalStateException will be shown. The solution:

When adding tab addtab, choose the tab to be not selected by setting setSelected params to false.

 actionBar.addTab(tab,false);

I think the exception appeared because of overriding the onTabSelected() method like the following

 viewPager.setCurrentItem(tab.getPosition());

So when calling addTab() it didn't recognize which viewPager and adapter to add the tab to.

add this line into your adapter file where the public Object instantiateItem(ViewGroup container, int position) method is called

((ViewPager) container).addView(view); return view;

Make sure the values in setOffscreenPageLimit and getCount match, otherwise you will get this error:

    mViewPager.setOffscreenPageLimit(MAX_TABS);

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    ...
    @Override
    public int getCount() {
        return MAX_TABS;
    }

Call Viewpager.setAdapter(adapter); after item add in your array list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!