Glide类似You cannot start a load for a destroyed activity异常简单分析

简介: Glide类似You cannot start a load for a destroyed activity异常简单分析

最近在做项目时,使用Glide加载网络图片时,碰到了 You cannot start a load for a destroyed activity 这个异常;


场景描述:点击进入一个Activity 当中请求网络 请求成功后 根据服务器返回的图片URL使用Glide来加载网络图片 ,当点击进入activity 加载网络过程中 退出activity 会报此错


今天有时间就索性研究下这个问题,就做个笔记,也希望能给同样碰到这个问题的小伙伴带来点帮助


先看下Glide的简单调用:


Glide.with(context).load(imageUrl).into(imageView);


根据异常的提示,我们可以确定问题应该是出在了Glide.with(context) 中的context

我们点到源码中看一下 Glide.with() 是怎么实现的。


public static RequestManager with(Context context) {
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(context);
    }
 public static RequestManager with(Activity activity) {
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(activity);
    }
  public static RequestManager with(FragmentActivity activity) {
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(activity);
    }
 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static RequestManager with(android.app.Fragment fragment) {
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(fragment);
    }
 public static RequestManager with(Fragment fragment) {
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(fragment);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

可以看到这里有很多的重构的方法,但是最终都会返回一个retriever.get(); 我们继续到retriever.get()里面去,我们会看到 RequestManagerRetriever 类。

1.public class RequestManagerRetriever implements Handler.Callback {
   ...
    public RequestManager get(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("You cannot start a load on a null Context");  //这里抛出了异常
        } else if (Util.isOnMainThread() && !(context instanceof Application)) {
            if (context instanceof FragmentActivity) {
                return get((FragmentActivity) context);
            } else if (context instanceof Activity) {
                return get((Activity) context);
            } else if (context instanceof ContextWrapper) {
                return get(((ContextWrapper) context).getBaseContext());
            }
        }
        return getApplicationManager(context);
    }
    public RequestManager get(FragmentActivity activity) {
        if (Util.isOnBackgroundThread()) {
            return get(activity.getApplicationContext());
        } else {
           assertNotDestroyed(activity); 
            FragmentManager fm = activity.getSupportFragmentManager();
            return supportFragmentGet(activity, fm);
        }
    }
    public RequestManager get(Fragment fragment) {
        if (fragment.getActivity() == null) {
            throw new IllegalArgumentException("You cannot start a load on a fragment before it is attached"); //这里抛出了异常
        } 
        if (Util.isOnBackgroundThread()) {
            return get(fragment.getActivity().getApplicationContext());
        } else {
            FragmentManager fm = fragment.getChildFragmentManager();
            return supportFragmentGet(fragment.getActivity(), fm);
        }
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public RequestManager get(Activity activity) {
        if (Util.isOnBackgroundThread() || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return get(activity.getApplicationContext());
        } else {
            assertNotDestroyed(activity); //检查activity的方法
            android.app.FragmentManager fm = activity.getFragmentManager();
            return fragmentGet(activity, fm); 
        }
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private static void assertNotDestroyed(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"); //这里抛出了异常
        }
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public RequestManager get(android.app.Fragment fragment) {
        if (fragment.getActivity() == null) {
            throw new IllegalArgumentException("You cannot start a load on a fragment before it is attached"); //这里抛出了异常
        }
        if (Util.isOnBackgroundThread() || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return get(fragment.getActivity().getApplicationContext());
        } else {
            android.app.FragmentManager fm = fragment.getChildFragmentManager();
            return fragmentGet(fragment.getActivity(), fm);
        }
    }
   ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

核心代码就在这里,我们看到也是有很多的重构方法,我们主要看这个方法assertNotDestroyed(Activity activity)image.png好了 找到Glide 抛出异常的地方了!


也就是当 activity.isDestroyed()为true的时候


同样的还有另外几个异常:

You cannot start a load on a fragment before it is attached


You cannot start a load on a null Context


归根结底都是因为我们传入了一个已经销毁的Activity或者是一个空的Context ,Fragment 挂载的Activity为空导致的


回顾使用的场景,是在联网请求成功之后调用的Glide 当执行到Glide.with();方法时,当前的Activity已经销毁了,所以才导致的这个问题。


我们尽量不要再非主线程里面使用Glide加载图片,这样容易导致抛出如You cannot start a load for a destroyed activity的异常,如果有需求的话,有一种解决方案是直接传入Application对象,这样就不会有这个问题了,但是使用Application对象会导致Glide加载图片的生命周期变长,当Activity已经销毁时,还在继续的加载图片,这样做会浪费很多的资源,所以我们还是简单的封装一个Glide加载的工具类来解决这个问题比较好。


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
/**
 * Glide 加载 简单判空封装 防止异步加载数据时调用Glide 抛出异常
 * Created by Li_Xavier on 2017/6/20 0020.
 */
public class GlideLoadUtils {
    private String TAG = "ImageLoader";
    /**
     * 借助内部类 实现线程安全的单例模式
     * 属于懒汉式单例,因为Java机制规定,内部类SingletonHolder只有在getInstance()
     * 方法第一次调用的时候才会被加载(实现了lazy),而且其加载过程是线程安全的。
     * 内部类加载的时候实例化一次instance。
     */
    public GlideLoadUtils() {
    }
    private static class GlideLoadUtilsHolder {
        private final static GlideLoadUtils INSTANCE = new GlideLoadUtils();
    }
    public static GlideLoadUtils getInstance() {
        return GlideLoadUtilsHolder.INSTANCE;
    }
    /**
     * Glide 加载 简单判空封装 防止异步加载数据时调用Glide 抛出异常
     *
     * @param context
     * @param url           加载图片的url地址  String
     * @param imageView     加载图片的ImageView 控件
     * @param default_image 图片展示错误的本地图片 id
     */
    public void glideLoad(Context context, String url, ImageView imageView, int default_image) {
        if (context != null) {
            Glide.with(context).load(url).centerCrop().error(default_image).crossFade
                    ().into(imageView);
        } else {
            Log.i(TAG, "Picture loading failed,context is null");
        }
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public void glideLoad(Activity activity, String url, ImageView imageView, int default_image) {
        if (!activity.isDestroyed()) {
            Glide.with(activity).load(url).centerCrop().error(default_image).crossFade
                    ().into(imageView);
        } else {
            Log.i(TAG, "Picture loading failed,activity is Destroyed");
        }
    }
    public void glideLoad(Fragment fragment, String url, ImageView imageView, int default_image) {
        if (fragment != null && fragment.getActivity() != null) {
            Glide.with(fragment).load(url).centerCrop().error(default_image).crossFade
                    ().into(imageView);
        } else {
            Log.i(TAG, "Picture loading failed,fragment is null");
        }
    }
    public void glideLoad(android.app.Fragment fragment, String url, ImageView imageView, int default_image) {
        if (fragment != null && fragment.getActivity() != null) {
            Glide.with(fragment).load(url).centerCrop().error(default_image).crossFade
                    ().into(imageView);
        } else {
            Log.i(TAG, "Picture loading failed,android.app.Fragment is null");
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

上面是我简单写的一个小工具类

这只是我针对项目中碰到的异常做的简单分析,如果你想要详细的研究下Glide的源码 请参考郭霖大大的博客:http://blog.csdn.net/guolin_blog/article/details/53939176

目录
相关文章
|
18天前
fragment启动activity方法
fragment启动activity方法
8 1
Activity中,View#postDelay会导致内存泄漏,但是不会影响Activity的生命周期执行。
Activity中,View#postDelay会导致内存泄漏,但是不会影响Activity的生命周期执行。
|
存储
OC:关于Category、load、initialize的那些事你还记得吗?
这篇文章主要分析Category的实现原理,load方法和initialize方法调用方式、调用时机、调用顺序、以及他们的区别,解释 Catgory 与 class Extension 有什么区别。
121 0
|
Java
Preference跳转activity出错Unable to find explicit activity class
使用Preference可以非常方便的实现类似设置页面这样的菜单布局,甚至可以不需写java代码。那么可以在Preference中直接添加页面跳转么?其实非常简单,在Preference添加intent标签即可
471 0
|
Kotlin
【错误记录】布局组件加载错误 ( Attempt to invoke virtual method ‘xxx$Callback android.view.Window.getCallback()‘ )
【错误记录】布局组件加载错误 ( Attempt to invoke virtual method ‘xxx$Callback android.view.Window.getCallback()‘ )
277 0
|
Android开发 容器
《Android的设计与实现:卷I》——第3章 3.5触发并启动Action和Service
本节书摘来自华章出版社《Android的设计与实现:卷I》——第3章,第3.5节。作者: 杨云君著.更多章节内容可以访问云栖社区“华章计算机”公众号查看。
1194 0
Activity finish 后 onDestroy ()并不会立马执行
只看标题就好 所以两个Activity在用到生命周期的时候,不要再onDestroy中做,控制不了
1000 0