ViewPager正常工作,在第一时间,但再重装,得到错误java.lang.IllegalStateException? [英] ViewPager works Fine at first time but on reloading again, getting the error java.lang.IllegalStateException?

查看:634
本文介绍了ViewPager正常工作,在第一时间,但再重装,得到错误java.lang.IllegalStateException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager的10幅图像来通过web服务(JSON),起初ViewPager工作进展顺利(精)。

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

但是当后面的活动,并重新打开它再次 我得到这个错误:

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 

在主Activity类

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.

我需要改变对工作

解决方案

早前,我路过的ArrayList但此时字符串数组**(工作)**真的,我不知道什么与ArrayList的问题

推荐答案

原因的问题的寻呼机可在一些测量和在这种情况下被刷新将刷新适配器的计数,如果该计数不与保存的计数等于你会看到错误。所以我的解决方案看起来:

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();
}

祝你好运。

这篇关于ViewPager正常工作,在第一时间,但再重装,得到错误java.lang.IllegalStateException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆