• 解决:Glide 在回调中再次加载图片报错


    一、问题说明

    Glide 加载图片时监听了回调,并在失败时再次加载其它图片后报错。

    代码:

    1. Glide.with(mContext).load(imgTeacher)
    2. .listener(new RequestListener() {
    3. @Override
    4. public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
    5. return false;
    6. }
    7. @Override
    8. public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
    9. try {
    10. Glide.with(mContext).load(imgTeacher).into(holder.iv2);
    11. } catch (Exception e) {
    12. e.printStackTrace();
    13. }
    14. return false;
    15. }
    16. })
    17. .into(holder.iv2);

    异常日志:

    1. java.lang.IllegalStateException: You can't start or clear loads in RequestListener or Target callbacks.
    2. 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.
    3. at com.bumptech.glide.request.SingleRequest.assertNotCallingCallbacks(SingleRequest.java:305)
    4. at com.bumptech.glide.request.SingleRequest.begin(SingleRequest.java:217)
    5. at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:806)
    6. at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:780)
    7. at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:771)
    8. at com.xueersi.lib.imageloader.GlideLoader.into(GlideLoader.java:321)
    9. at com.xueersi.lib.imageloader.GlideLoader.request(GlideLoader.java:250)
    10. at com.xueersi.lib.imageloader.SingleConfig.show(SingleConfig.java:270)
    11. at com.xueersi.lib.imageloader.SingleConfig.access$2700(SingleConfig.java:23)
    12. at com.xueersi.lib.imageloader.SingleConfig$ConfigBuilder.into(SingleConfig.java:347)

    二、解决方案

    hander.post 一下:

    1. Glide.with(mContext).load("")
    2. .listener(new RequestListener() {
    3. @Override
    4. public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
    5. holder.iv1.post(() -> {
    6. Glide.with(mContext).load(imgTeacher).into(holder.iv2);
    7. });
    8. return false;
    9. }
    10. @Override
    11. public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
    12. return false;
    13. }
    14. })
    15. .into(holder.iv2);

  • 相关阅读:
    YOLOv5图像分割--SegmentationModel类代码详解
    SpringCloud01
    农产品果蔬商城交易系统(Java+Web+MySQL)
    基于php网上零食商店管理系统获取(php毕业设计)
    字字珠玑!GitHub爆赞的网络协议手册,被华为大佬指定内部必学?
    ORM数据库查询操作
    GitLab仓库管理系统安装详细步骤
    Go 将在下个版本支持新型排序算法:pdqsort
    【Linux·克莱因计划】实用指令操作汇总合集(上)
    如何使用 Node.js和Express搭建服务器?
  • 原文地址:https://blog.csdn.net/haha223545/article/details/136198558