2

最近在友盟收集的错误列表中,发现有个问题使得蛮多用户闪退的。根据错误信息定位到,是由于图片轮播控件com.youth.banner使用Glide异步加载图片时发生的崩溃。在开发及测试过程中,并没有发生这个问题,话不多说,直接分析错误信息。

1.首先来看看崩溃日记:

java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
    at com.b.a.e.m.b(RequestManagerRetriever.java:311)
    at com.b.a.e.m.a(RequestManagerRetriever.java:130)
    at com.b.a.e.m.a(RequestManagerRetriever.java:114)
    at com.b.a.d.c(Glide.java:697)
    at com.sami91sami.h5.c.ag$b.a(MainFragment.java:1079)
    at com.sami91sami.h5.c.ag$b.displayImage(MainFragment.java:1063)
    at com.youth.banner.Banner.setImageList(Banner.java:354)
    at com.youth.banner.Banner.start(Banner.java:262)
    at com.sami91sami.h5.c.ag.a(MainFragment.java:914)
    at com.sami91sami.h5.c.ag.a(MainFragment.java:1223)
    at com.sami91sami.h5.c.ag.a(MainFragment.java:90)
    at com.sami91sami.h5.c.bg.a(MainFragment.java:1202)
    at com.sami91sami.h5.c.bg.a(MainFragment.java:1183)
    at com.zhy.a.a.e.run(OkHttpUtils.java:171)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:232)
    at android.app.ActivityThread.main(ActivityThread.java:7225)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:500)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:908)

2.根据错误信息找到发生闪退的代码位置:

    //自定义的图片加载器
    private class ImgLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {        
            RoundedCorners roundedCorners = new RoundedCorners(20);
            RequestOptions options = new RequestOptions().bitmapTransform(roundedCorners);
            //报错地方
            Glide.with(context).load((String) path).apply(options).into(imageView);
        }
    }

3.跟踪日志进入Glide调用的地方发现,出现在RequestManagerRetriever.assertNotDestroyed()方法中:

    @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");
        }
    }

4.这个错误是使用Glide异步加载图片的时候,Activity已经Destroyed。

5.知道了错误原因,下面我们来看看解决方案:

5.1).在使用Glide加载图片前,先进行Activity是否Destroy的判断:

    /**
     * 判断Activity是否Destroy
     * @param activity
     * @return
     */
    public static boolean isDestroy(Activity mActivity) {
        if (mActivity== null || mActivity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) {
            return true;
        } else {
            return false;
        }
    }

5.2).在错误的位置进行替换:

    //自定义的图片加载器
    private class ImgLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) { 
            //添加判断 
            if(!isDestroy((Activity)context)){
                  RoundedCorners roundedCorners = new RoundedCorners(20);
                RequestOptions options = new RequestOptions().bitmapTransform(roundedCorners);
                Glide.with(context).load((String) path).apply(options).into(imageView);
                }
        }
    }

6.到这里问题就解决了。


以下是公众号(longxuanzhigu),之后发布的文章会同步到该公众号,方便交流学习Android知识,欢迎关注:
在这里插入图片描述


龙旋
39 声望2 粉丝