gpt4 book ai didi

java - GridView 试图从边界外获取 ArrayList 数据

转载 作者:太空狗 更新时间:2023-10-29 13:50:23 25 4
gpt4 key购买 nike

我正在制作 GridView 小部件,它加载从 ArrayList 中获取的图像,其中有 40 张。这些对象从内存加载到 gridView 非常好,但是当我向下滚动小部件时,getViewAt 方法想要加载更多对象,这让我遇到了 OutOfBounds Exception。但是我的方法包含 if 语句,在这种情况下应该返回 null 值,但可能有一些错误,这是我的 GetViewAt 方法:

@Override
public RemoteViews getViewAt(int position) {
if (position == AdapterView.INVALID_POSITION ||
cursor == null || !cursor.moveToPosition(position)) {
return null;
}
RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.widget_item);
Bitmap img = resizeBitmapFitXY(250, 150, photos.get(position));
views.setImageViewBitmap(R.id.imageView, img);
return views;
}

最佳答案

问题

where is the OutOfBounds Exception

回答

it is on this line: Bitmap img = resizeBitmapFitXY(250, 150, photos.get(position))

解决方案

将此添加到 if 语句中

if (photos == null || photos.size() == 0 || position >= photos.size()){
return null;
}

关于java - GridView 试图从边界外获取 ArrayList 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621381/

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