最近在友盟收集的錯誤列表中,發現有個問題使得蠻多用戶閃退的。根據錯誤信息定位到,是因爲圖片輪播控件com.youth.banner使用Glide異步加載圖片時發生的崩潰。在開發及測試過程當中,並無發生這個問題,話很少說,直接分析錯誤信息。java
1.首先來看看崩潰日記:android
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.根據錯誤信息找到發生閃退的代碼位置:app
//自定義的圖片加載器
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。ide
5.知道了錯誤緣由,下面咱們來看看解決方案:oop
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.到這裏問題就解決了。ui
如下是公衆號(longxuanzhigu),以後發佈的文章會同步到該公衆號,方便交流學習Android知識,歡迎關注: spa