写了一个无限循环的ViewPager,原理是在最左和最右各增加一个View,用于滑动。但是跑Monkey的时候爆出:
CRASH: com.wzy.test(pid 11400)
// Short Msg: java.lang.IndexOutOfBoundsException保存
// Long Msg: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
// Build Changelist: 1475158271
// Build Time: 1475158954000
// java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
// at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:260)
// at java.util.ArrayList.get(ArrayList.java:313)
// at com.wzy.test.widget.MarqueeContainer$MarqueePagerAdapter.destroyItem(MarqueeContainer.java:230)
// at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:474)
// at com.wzy.test.widget.MarqueeContainer.showMarquee(MarqueeContainer.java:169)
// at com.wzy.test.widget.MarqueeContainer.setImagesUrl(MarqueeContainer.java:76)
// at com.wzy.test.activity.DetailActivity$1.handleMessage(DetailActivity.java:62)
// at android.os.Handler.dispatchMessage(Handler.java:111)
// at android.os.Looper.loop(Looper.java:205)
// at android.app.ActivityThread.main(ActivityThread.java:5811)
// at java.lang.reflect.Method.invoke(Native Method)
// at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
// at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
很奇怪,理论上一共就有三个item,destoryItem的position不应该为3.
实现的PagerAdapter:
public class MarqueePagerAdapter extends PagerAdapter {
/**
* 获取要滑动控件的数量.我们的自定义控件用来实现图片的滑动,因此这里就应该是展示的图片的数量.
*/
@Override
public int getCount() {
return mDisplayViewList == null ? 0 : mDisplayViewList.size();
}
/**
* 来判断显示的是否是同一张图片,我们直接将两个参数进行比较即可.
*/
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
/**
* 当要显示的图片可以进行缓存的时候,会调用这个方法进行显示图片的初始化.
* 我们将要显示的ImageView添加到container中,然后将该ImageView返回即可.
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mDisplayViewList.get(position));
return mDisplayViewList.get(position);
}
/**
* ViewPager默认只缓存三张要显示的图片,如果滑动的图片超过了缓存的范围,就会调用这个方法将图片移除
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mDisplayViewList.get(position));
}
}
求指导一下,大家是怎么解决这个问题的?(ps:没有调用过notifyDataSetChanged方法)。
The problem is that the array is out of bounds
Make a log in the destroyItem method to see the actual data num of mDisplayViewList.size(). I guess your mDisplayViewList data has changed elsewhere in the code. In addition, what is the specific data of mDisplayViewList?
Solution provided Reference