java.lang.IndexOutOfBoundsException:尝试从 RecyclerView 中删除损坏的 URL 项目
java.lang.IndexOutOfBoundsException : Trying to remove an item with broken URL from RecyclerView
我正在使用 Glide 加载 URL 的列表,如下所示:
@Override
public void onBindViewHolder(@NonNull SearchViewHolder holder, int position) {
Glide.with(context)
.load(arrayList.get(position))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.drawable.transparent_icon)
.override(Constants.THUMBNAIL_SIZE, Constants.THUMBNAIL_SIZE)
.fitCenter()
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
isBrokenUrl = true;
arrayList.remove(holder.getBindingAdapterPosition()); //ISSUE IS HERE
notifyItemRangeChanged(holder.getBindingAdapterPosition(), getItemCount());
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
isBrokenUrl = false;
return false;
}
}).into(holder.image);
holder.image.setOnClickListener(v -> itemStringClickListener.onItemClicked(arrayList.get(position)));
}
列表中有损坏的 URL,我想从 RecyclerView
中删除带有这些损坏 url 的项目:
arrayList.remove(holder.getBindingAdapterPosition());
notifyItemRangeChanged(holder.getBindingAdapterPosition(), getItemCount());
它可以工作,但如果我滚动得太快,应用程序会崩溃并显示:
com.bumptech.glide.load.engine.CallbackException: Unexpected exception thrown by non-Glide code
at com.bumptech.glide.load.engine.EngineJob.callCallbackOnLoadFailed(EngineJob.java:175)
at com.bumptech.glide.load.engine.EngineJob$CallLoadFailed.run(EngineJob.java:402)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7889)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=109; index=-1
at java.util.ArrayList.remove(ArrayList.java:506)
at maa.utils.adapters.SearchedStickersAdapter.onLoadFailed(SearchedStickersAdapter.java:63)
at com.bumptech.glide.request.SingleRequest.onLoadFailed(SingleRequest.java:683)
at com.bumptech.glide.request.SingleRequest.onLoadFailed(SingleRequest.java:651)
at com.bumptech.glide.load.engine.EngineJob.callCallbackOnLoadFailed(EngineJob.java:173)
at com.bumptech.glide.load.engine.EngineJob$CallLoadFailed.run(EngineJob.java:402)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7889)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
代码失败,因为 getBindingAdapterPosition 返回 -1。该视图持有者不受任何位置的约束。看起来你想在这里做一些异步的事情。如果是这样,您不应该将视图持有者或其中的任何视图传递给任何回调。相反,您应该在进行异步调用时将位置 and/or 处的数据项传递给该索引,而不是在回调期间查询持有者。这是因为绑定可以在 then 和回调发生之间发生变化。如果你不这样做,即使它“有效”,它也可能在错误的索引上执行操作。
实际上,由于您是在 onBindViewHolder 中执行此操作,因此在您使用它的两个地方仅将其替换为 position
应该可以正常工作。
我正在使用 Glide 加载 URL 的列表,如下所示:
@Override
public void onBindViewHolder(@NonNull SearchViewHolder holder, int position) {
Glide.with(context)
.load(arrayList.get(position))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.drawable.transparent_icon)
.override(Constants.THUMBNAIL_SIZE, Constants.THUMBNAIL_SIZE)
.fitCenter()
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
isBrokenUrl = true;
arrayList.remove(holder.getBindingAdapterPosition()); //ISSUE IS HERE
notifyItemRangeChanged(holder.getBindingAdapterPosition(), getItemCount());
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
isBrokenUrl = false;
return false;
}
}).into(holder.image);
holder.image.setOnClickListener(v -> itemStringClickListener.onItemClicked(arrayList.get(position)));
}
列表中有损坏的 URL,我想从 RecyclerView
中删除带有这些损坏 url 的项目:
arrayList.remove(holder.getBindingAdapterPosition());
notifyItemRangeChanged(holder.getBindingAdapterPosition(), getItemCount());
它可以工作,但如果我滚动得太快,应用程序会崩溃并显示:
com.bumptech.glide.load.engine.CallbackException: Unexpected exception thrown by non-Glide code
at com.bumptech.glide.load.engine.EngineJob.callCallbackOnLoadFailed(EngineJob.java:175)
at com.bumptech.glide.load.engine.EngineJob$CallLoadFailed.run(EngineJob.java:402)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7889)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=109; index=-1
at java.util.ArrayList.remove(ArrayList.java:506)
at maa.utils.adapters.SearchedStickersAdapter.onLoadFailed(SearchedStickersAdapter.java:63)
at com.bumptech.glide.request.SingleRequest.onLoadFailed(SingleRequest.java:683)
at com.bumptech.glide.request.SingleRequest.onLoadFailed(SingleRequest.java:651)
at com.bumptech.glide.load.engine.EngineJob.callCallbackOnLoadFailed(EngineJob.java:173)
at com.bumptech.glide.load.engine.EngineJob$CallLoadFailed.run(EngineJob.java:402)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7889)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
代码失败,因为 getBindingAdapterPosition 返回 -1。该视图持有者不受任何位置的约束。看起来你想在这里做一些异步的事情。如果是这样,您不应该将视图持有者或其中的任何视图传递给任何回调。相反,您应该在进行异步调用时将位置 and/or 处的数据项传递给该索引,而不是在回调期间查询持有者。这是因为绑定可以在 then 和回调发生之间发生变化。如果你不这样做,即使它“有效”,它也可能在错误的索引上执行操作。
实际上,由于您是在 onBindViewHolder 中执行此操作,因此在您使用它的两个地方仅将其替换为 position
应该可以正常工作。