gpt4 book ai didi

java.lang.IllegalStateException : ViewPager 错误

转载 作者:行者123 更新时间:2023-11-30 01:37:01 25 4
gpt4 key购买 nike

我的应用程序从服务器获取所有图像 URL 并将其保存到 ArrayList 并在 ViewPager 中显示这些图像。但它会生成一个 IllegalStateException。适配器如下:

public class FullScreenImageAdapter extends PagerAdapter {

private Context _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;

// constructor
public FullScreenImageAdapter(Context activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}

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

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

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

inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.item, container,
false);

imgDisplay = (ImageView) viewLayout.findViewById(R.id.cardImage);

Picasso.with(_activity).load(_imagePaths.get(position)).into(imgDisplay);


((ViewPager) container).addView(viewLayout, 0);

return viewLayout;
}

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

}
}

适配器创建为

FullScreenImageAdapter adapter=new FullScreenImageAdapter(FullScreenActivity.this,all_url);


viewPager.setAdapter(adapter);

日志如下所示:

 java.lang.IllegalStateException: The application's PagerAdapter
> changed the adapter's contents without calling
> PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1,
> found: 3 Pager id: com.wat.clickzy:id/view_pager Pager class: class
> android.support.v4.view.ViewPager Problematic adapter: class
> com.wat.clickzy.FullScreenImageAdapter

请帮帮我

最佳答案

您需要在您正在使用的适配器上调用notifysetdatachanged,每次您向该适配器添加/删除内容时.

here为了更清楚。

关于java.lang.IllegalStateException : ViewPager 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34989546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com