ViewPager works Fine at first time but on reloading again, getting the error java.lang.IllegalStateException?(ViewPager 第一次工作正常,但再次重新加载时,出现错误 java.lang.IllegalStateException?)
问题描述
我有一个 ViewPager,其中 10 张图片来自 webservices(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();
我从 stackoverflow 获得但无法工作的解决方案.
the solution I got from stackoverflow but couldn't work.
我需要改变什么才能工作?
解决方案
之前我在传递 ArrayList 但是这次 String Array **(working )** 真的我不知道 ArrayList 有什么问题
推荐答案
问题的原因 Pager 可以在某些测量期间刷新,在这种情况下将刷新您的适配器的计数,如果此计数不等于您保存的计数会看到错误.所以我的解决方案看起来:
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ViewPager 第一次工作正常,但再次重新加载时,出现错误 java.lang.IllegalStateException?
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01