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

前端 未结 10 1994
感情败类
感情败类 2020-11-29 06:52

I\'m using the ViewPager example with ActionBar tabs taken from the Android documentation here.

Unfortunately, as soon as I call the

相关标签:
10条回答
  • 2020-11-29 07:32

    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.

    0 讨论(0)
  • 2020-11-29 07:39

    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.

    0 讨论(0)
  • 2020-11-29 07:39

    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;

    0 讨论(0)
  • 2020-11-29 07:42

    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.

    0 讨论(0)
  • 2020-11-29 07:48

    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();
    
            }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题