Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

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

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 again I got this error :

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! 

Expected adapter item count: 10, found: 0 Pager id: com.akm.demo.activities:id/slideShowPager Pager class: class android.support.v4.view.ViewPager 

In main Activity class

if (mSlideShowData != null) { // mSlideShowData is An ArrayList of the Images Which I pass to ImagePagerAdapter
adapter = new ImagePagerAdapter(getContext(), mSlideShowData);
slideShowPager.setAdapter(adapter);

//slideShowPager.invalidate();
((PagerAdapter)slideShowPager.getAdapter()).notifyDataSetChanged(); 
}

 else{
Toast toast = Toast.makeText(getContext(), "No Record Found", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();

ImagePagerAdapter.java

public class ImagePagerAdapter extends PagerAdapter {

private Context mContext;
private ImageLoader mImageLoader; // It's use for downloading the image from server
private ArrayList<SlideShowData> slideShowImages; // In this I have all the detail of the image from  API

public ImagePagerAdapter(Context context, ArrayList<SlideShowData> mSlideShowData) {
    mContext = context;
    slideShowImages = mSlideShowData;
    mImageLoader = new ImageLoader(mContext);

}

@Override
public int getItemPosition(Object object) {
     return POSITION_NONE;
}

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

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((ImageView) object);
}



@Override
public Object instantiateItem(ViewGroup container, int position) {

    ImageView imageViewSlideShow = new ImageView(mContext);
    int padding = mContext.getResources().getDimensionPixelSize(R.dimen.padding_medium);
    imageViewSlideShow.setPadding(padding, padding, padding, padding);
    imageViewSlideShow.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    mImageLoader.DisplayImage(ServiceURLs.URL+"/photos/"+slideShowImages.get(position).photo_name, R.drawable.loading, imageViewSlideShow);
    ((ViewPager) container).addView(imageViewSlideShow, 0);
    return imageViewSlideShow;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((ImageView) object);

}

@Override
public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
}

}

For solution , I tried :

@Override
public int getItemPosition(Object object) {
     return POSITION_NONE;
}

//And

slideShowPager.getAdapter().notifyDataSetChanged();

the solution I got from stackoverflow but couldn't work.

what i need to changes for working ?

Solution

earlier I am Passing the ArrayList But this time String Array **(working )** Really I don't know whats the problem with the ArrayList

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...