ViewPager works Fine at first time but on reloading again, getting the error java.lang.IllegalStateException?

前端 未结 2 1304
时光说笑
时光说笑 2020-12-16 00:56

I have a ViewPager with the 10 images comes through webservices(JSON), At first ViewPager work smoothly (fine).

but When back from the activity and reopen it

相关标签:
2条回答
  • 2020-12-16 01:48

    Reason of the problem the Pager can be refreshed during some measurement and in this case will refresh count of your adapter, if this count will not equals with saved count you will see the error. So my solution looks:

    public abstract class ViewPagerCursorAdapter extends PagerAdapter {
    
        private int mCount;
    
        public ViewPagerCursorAdapter(Context ctx, Cursor cursor, int resource) {
            super();
            ...
            mCount = cursor.getCount();
            ...
        }
    
        @Override
        public int getCount() {
            return mCount;
        }
    
        public Cursor swapCursor(Cursor newCursor) {
            ...
            mCount = newCursor.getCount();
            notifyDataSetChanged();
        }
    }
    

    Good luck.

    0 讨论(0)
  • 2020-12-16 01:52

    The answer they gave worked for a similar problem of mine.. I am just in production, have no idea how all these codes work, But what they said kind of helped me in the other thread of yours. You have to notify like they said, in the places where you set or delete your images.

    So, I think here your problem is that you have to make a notify after you onCreateView() and also in your destroyItem() .. I would add the already suggested solution here.. I guess that should do it.. hope I am not misguiding you.. good luck..

         slideShowPager.getAdapter().notifyDataSetChanged();
    

    td:lr; That line in two of your functions onCreateView() and destroyItem() in the end might do the trick

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