Glide加载RecyclerView图片失败

本来是比较简单的一个东西,在自定义适配器的onBindViewHolder()方法中直接一句话:

Glide.with(mcontext).load(img).into(holder.newsImg);

可就是这个context坑了一波,这里的mcontext我是定义的成员变量?(全局变量),放在这里没报错以为就是对的。但一加载就闪退,报错如下:


You cannot start a load on a not yet attached View or a  Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).

百度了一波都是和什么activity和fragment生命周期有关,看了几个没懂,决定再试试,于是将这个mcontext打印了一下:


果然是null,于是就去找可以用的context,最后参考上面onCreateViewHolder()中启动intent用到的context

Glide加载RecyclerView图片失败_第1张图片

用了holder.itemView.getContext(),就成功加载啦!!!

看了下这个itemview是RecyclerView里面ViewHolder内部类的属性,我也不知道是什么意思,有空再研究啦:

Glide加载RecyclerView图片失败_第2张图片

你可能感兴趣的:(安卓开发,errors,Glide)