一、问题说明
Glide 加载图片时监听了回调,并在失败时再次加载其它图片后报错。
代码:
Glide.with(mContext).load(imgTeacher)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
try {
Glide.with(mContext).load(imgTeacher).into(holder.iv2);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
})
.into(holder.iv2);
异常日志:文章来源:https://www.toymoban.com/news/detail-834759.html
java.lang.IllegalStateException: You can't start or clear loads in RequestListener or Target callbacks.
If you're trying to start a fallback request when a load fails, use RequestBuilder#error(RequestBuilder). Otherwise consider posting your into() or clear() calls to the main thread using a Handler instead.
at com.bumptech.glide.request.SingleRequest.assertNotCallingCallbacks(SingleRequest.java:305)
at com.bumptech.glide.request.SingleRequest.begin(SingleRequest.java:217)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:806)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:780)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:771)
at com.xueersi.lib.imageloader.GlideLoader.into(GlideLoader.java:321)
at com.xueersi.lib.imageloader.GlideLoader.request(GlideLoader.java:250)
at com.xueersi.lib.imageloader.SingleConfig.show(SingleConfig.java:270)
at com.xueersi.lib.imageloader.SingleConfig.access$2700(SingleConfig.java:23)
at com.xueersi.lib.imageloader.SingleConfig$ConfigBuilder.into(SingleConfig.java:347)
二、解决方案
hander.post 一下:文章来源地址https://www.toymoban.com/news/detail-834759.html
Glide.with(mContext).load("")
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
holder.iv1.post(() -> {
Glide.with(mContext).load(imgTeacher).into(holder.iv2);
});
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(holder.iv2);
到了这里,关于解决:Glide 在回调中再次加载图片报错的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!