gpt4 book ai didi

android - 填充 RecyclerView 时出现 ArrayIndexOutOfBoundsException

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:12 48 4
gpt4 key购买 nike

当我尝试填充 RecyclerView 时,我经常遇到错误,但错误似乎发生在 StaggeredGridLayoutManager 内部。

我从我的数据库中填充 RecyclerView,然后使用这个将图像添加到我的适配器

List<WImage> images;

public void addImages(LinkedHashMap<Integer,WImage> newImages){
for(Map.Entry<Integer,WImage> entry : newImages.entrySet()){
addImage(entry.getValue(),entry.getKey());
}
}

public void addImage(WImage image, int position){
images.add(position,image);
notifyItemRangeInserted(position, 1);

}

奇怪的是,当应用程序首次加载时它很好并正确显示所有内容,但如果我根据数据库查询将 RecyclerView 中当前的内容替换为一组不同的图像,然后再次返回初始图像,我得到这个错误。

java.lang.ArrayIndexOutOfBoundsException: src.length=16 srcPos=0 dst.length=0 dstPos=0 length=16
at java.lang.System.arraycopy(Native Method)
at android.support.v7.widget.StaggeredGridLayoutManager$LazySpanLookup.ensureSize(StaggeredGridLayoutManager.java:2277)
at android.support.v7.widget.StaggeredGridLayoutManager$LazySpanLookup.offsetForAddition(StaggeredGridLayoutManager.java:2323)
at android.support.v7.widget.StaggeredGridLayoutManager.handleUpdate(StaggeredGridLayoutManager.java:1280)
at android.support.v7.widget.StaggeredGridLayoutManager.onItemsAdded(StaggeredGridLayoutManager.java:1253)
at android.support.v7.widget.RecyclerView$5.dispatchUpdate(RecyclerView.java:406)
at android.support.v7.widget.RecyclerView$5.onDispatchSecondPass(RecyclerView.java:422)
at android.support.v7.widget.AdapterHelper.consumePostponedUpdates(AdapterHelper.java:119)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1896)
at android.support.v7.widget.RecyclerView.resumeRequestLayout(RecyclerView.java:1101)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:155)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

它很好地浏览了所有图像,但是当它准备好再次调用 onBindViewHolder 时它崩溃了。

当我更改 RecyclerView 中的数据时,我先删除所有内容,然后再填充查询中的新数据

public void removeAll(){
images.clear();
notifyDataSetChanged();
}

我也试过一次全部插入它们,然后用起始位置和图像总数调用 notifyItemRangeInserted,但它仍然因同样的错误而崩溃。

编辑

当我点击大约 20 个左右需要放入网格的项目时,它似乎会发生。如果我只是使用 notifyDatasetChanged 它似乎工作正常但我想使用 notifyItemRangeInserted 所以我得到了漂亮的插入动画

还有其他人遇到过这个问题吗?

EDIT2

这是我的onCreateViewHolder

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_normal_grid_cell, viewGroup, false);
return new ViewHolder(v);
}

和我的onBindViewHolder

@Override
public void onBindViewHolder(final ViewHolder holder, final int i) {
final WImage image = images.get(i);
holder.v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(v,i,image);
}
});

holder.heart.setActivated(image.getLikeDislike());

holder.heart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri.Builder ub = SyncProvider.CONTENT_ID_URI_BASE.buildUpon();
ub.appendPath(String.valueOf(image.getId()));
ContentValues values = new ContentValues();

if(image.getLikeDislike()){
values.put(SyncProvider.LIKE,0);
holder.heart.setActivated(false);
image.setLikeDislike(false);
}else{
values.put(SyncProvider.LIKE,1);
holder.heart.setActivated(true);
image.setLikeDislike(true);
}
values.put(SyncProvider.IMAGE_CHANGED,1);
context.getContentResolver().update(ub.build(),values,null,null);
}
});
if(cancelPotentialDownload(image.getUri(),holder.imageview)){
LoadImageTask task = new LoadImageTask(holder.imageview,image.getThumbUri());
holder.imageview.setImageDrawable(new AsyncBitmap(task));
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}

编辑 3

这是我更换适配器时所做的

在我的抽屉导航的选定索引更改时,我初始化我的加载程序以进行查询

public void initLoader(int page){
if(currentPage != page && mAdapter != null){
mAdapter.removeAll();
hashImages.clear();
getLoaderManager().destroyLoader(currentPage);
}
currentPage = page;
getLoaderManager().initLoader(page,null,this);
}

然后在加载程序的 onLoadFinished 中将对象添加到适配器

@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
if(cursor != null && cursor.moveToFirst()){
do{
WImage image = new WImage(cursor);
if(!hashImages.containsKey(image.getId())){
newImages.put(tempHash.size(),image);
}else if(image.getImageChanged()){
update = true;
}
int pos = tempHash.size();
tempHash.put(image.getId(),pos);
}while(cursor.moveToNext());
}

if(newImages.size() > 0){
mAdapter.addImages(newImages);
}

最佳答案

Google 在 21.0.2 版本中已解决此错误:

compile "com.android.support:support-v4:21.0.2"
compile "com.android.support:recyclerview-v7:21.0.2"

关于android - 填充 RecyclerView 时出现 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26662525/

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