今天,在RecyclerView适配器中使用Glide加载图片,出现这个问题,程序闪退,然后报以下异常。
java.lang.NullPointerException: 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).
翻译:空指针异常:不能在getActivity()返回null的未附加视图或片段上启动加载(通常在片段被附加之前或片段被销毁之后调用getActivity())。
在项目中一般都是这样写:
本来是比较简单的一个东西,为什么会这样呢?我尝试用了很多方法。但一加载就闪退!
解决方法:
用了holder.itemView.getContext(),就成功加载啦!!!