You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Glide无法从Room数据库加载图片,报错Load failed for null with size [0x0]

错误产生原因

  • 参数类型不匹配:Glide的load()方法不支持直接传入图片路径List,你在适配器中直接将getImages()返回的List<String>作为参数传入,Glide无法识别该类型,判定为null模型,触发报错。
  • 空值隐患:DTO类的getImages()方法没有处理this.images字段为空的情况,当Room中存储的图片字段为空时,Gson.fromJson会返回null,也会导致传入Glide的参数为空。
  • 依赖冗余:你重复声明了多次Glide依赖,可能引发类冲突,增加运行时异常概率。

解决方案

1. 修改适配器图片加载逻辑

取图片列表的第一个元素传入Glide,同时增加空判断,代码如下:

@Override
public void onBindViewHolder(@NonNull reminderViewHolder holder, int position) {
    List<String> imageList = rems.get(position).getImages();
    String targetImage = null;
    if (imageList != null && !imageList.isEmpty()) {
        targetImage = imageList.get(0);
    }
    GlideApp.with(hostFragment)
            .load(targetImage)
            .placeholder(R.drawable.ic_launcher_background)
            .fitCenter()
            .into(holder.binding.imageView);
    // 原有其余逻辑保持不变
    holder.binding.textrow.setText(rems.get(position).getTextRem());
    holder.binding.daterow.setText(rems.get(position).getDateRem());
    holder.binding.isdone.setChecked(rems.get(position).isDone());
}

2. 优化DTO空值处理逻辑

补全getImages()方法的空值分支,避免返回null:

@Override
public List<String> getImages() {
    if (super.getImages() == null || super.getImages().isEmpty()) {
        if (this.images != null && !this.images.isEmpty()) {
            super.setImages(new Gson().fromJson(this.images, List.class));
        } else {
            super.setImages(Collections.emptyList());
        }
    }
    return super.getImages();
}

3. 清理冗余依赖

删除重复的Glide依赖声明,仅保留如下配置即可:

implementation ("com.github.bumptech.glide:glide:4.12.0") {
    exclude group: "com.android.support"
    transitive = true
}
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

内容的提问来源于stack exchange,提问作者Spectator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 20:09:02