当前位置: 代码迷 >> 综合 >> 解决 java.lang.IllegalArgumentException:You cannot start a load for a destroyed activity 造成的闪退问题
  详细解决方案

解决 java.lang.IllegalArgumentException:You cannot start a load for a destroyed activity 造成的闪退问题

热度:26   发布时间:2023-11-17 10:41:16.0

序 、今天周一就跟我来了一个大事故 。太 TMD 难了 。

 

报错内容

Caused by:
5 java.lang.IllegalArgumentException:You cannot start a load for a destroyed activity
6 com.bumptech.glide.b.l.c(RequestManagerRetriever.java:323)
7 com.bumptech.glide.b.l.a(RequestManagerRetriever.java:132)
8 com.bumptech.glide.b.l.a(RequestManagerRetriever.java:116)
9 com.bumptech.glide.d.c(Glide.java:707)

定位代码(Glide)

  @NonNullpublic static RequestManager with(@NonNull Context context) {return getRetriever(context).get(context);} @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)private static void assertNotDestroyed(@NonNull Activity activity) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {throw new IllegalArgumentException("You cannot start a load for a destroyed activity");}}

PS:报这个错的原因是 activity.isDestoryed ()  ,就是如果Glide 使用所在的 Activity 已经销毁的话 ,就会报这个错 。

 

项目代码

public class VideoLoadingDialog extends BaseDialogFragment{private ImageView iv_loading;@Overrideprotected int setLayoutId() {return R.layout.dialog_videolaoding;}@Overrideprotected void initView(View view) {super.initView(view);iv_loading = view.findViewById(R.id.iv_loading);Glide.with(getContext()).load(R.drawable.waiting).into(iv_loading);}@Overridepublic void onDestroy() {super.onDestroy();if(iv_loading != null){Glide.with(getContext()).clear(iv_loading);iv_loading = null;}}
}

PS:为了解决一个内存泄漏问题 。在 DialogFragment 的 onDestory ()方法里面做了Glide 的 clear 。

 

解决方案  做一个判断 。

   @Overridepublic void onDestroy() {super.onDestroy();if(iv_loading != null &&  !getActivity().isDestroyed()){Glide.with(getContext()).clear(iv_loading);iv_loading = null;}}

 

 

PS:这是一个小瑕疵  ,切记 。(以后改东西 ,一定要好好做测试 ,可能会发生你想象不到的玄幻结果)