Luf*_*ffy 46 android android-glide
我使用relativelayout设置图像.为什么我没有使用imageview手段,在relativelayout图像内,我正在设置图标.
我不知道滑翔中究竟是什么问题.我已经发布了下面的堆栈跟踪和相关代码:
logcat的:
FATAL EXCEPTION: main
Process: com.app.steve, PID: 15928
java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:134)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:102)
at com.bumptech.glide.Glide.with(Glide.java:644)
at com.app.steve.TabMorePagesDetailActivity$allPageDetails.onPostExecute(TabMorePagesDetailActivity.java:1050)
at com.app.steve.TabMorePagesDetailActivity$allPageDetails.onPostExecute(TabMorePagesDetailActivity.java:885)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Run Code Online (Sandbox Code Playgroud)
TabMorePagesDetailActivity.java:
RelativeLayout rlPageCoverImg;
rlPageCoverImg = (RelativeLayout)findViewById(R.id.rl_club_cover_img);
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
dialog.dismiss();
............
String coverIMGurl = cover_avatar_obj.getString("url");
Log.e("ImgURL", coverIMGurl);
Glide.with(TabMorePagesDetailActivity.this).load(coverIMGurl).asBitmap().signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.into(new SimpleTarget<Bitmap>(500, 500) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Drawable drawable = new BitmapDrawable(getResources(), resource);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
rlPageCoverImg.setBackground(drawable);
}
}
});
}else {
rlPageCoverImg.setBackgroundResource(R.drawable.bg_golive);
}
@Override
protected void onDestroy()
{
super.onDestroy();
Glide.clear(rlPageCoverImg);
}
Run Code Online (Sandbox Code Playgroud)
layout.xml:
<RelativeLayout
android:id="@+id/rl_club_cover_img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/cancel_image" >
// Inside this relativelayout image, I'm using buttons and icons
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Fer*_*med 102
使用:
Glide.with(getApplicationContext()).load(...)
代替:
Glide.with(TabMorePagesDetailActivity.this).load(...)
希望它能解决你的问题〜
Set*_*san 13
灵感来自 GitHub线程,我在加载任何图像之前使用它
final Context context = getApplication().getApplicationContext();
if (isValidContextForGlide(context)){
// Load image via Glide lib using context
}
public static boolean isValidContextForGlide(final Context context) {
if (context == null) {
return false;
}
if (context instanceof Activity) {
final Activity activity = (Activity) context;
if (activity.isDestroyed() || activity.isFinishing()) {
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
DiR*_*oiD 11
您可以简单地检查上下文是否被销毁;
if (context == null) {
return
} else if (context !is Application) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (context is FragmentActivity) {
if ((context as FragmentActivity).isDestroyed) {
return
}
} else if (context is Activity) {
if ((context as Activity).isDestroyed) {
return
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这也可以表示为Kotlin扩展函数:
/**
* Return true if this [Context] is available.
* Availability is defined as the following:
* + [Context] is not null
* + [Context] is not destroyed (tested with [FragmentActivity.isDestroyed] or [Activity.isDestroyed])
*/
fun Context?.isAvailable(): Boolean {
if (this == null) {
return false
} else if (this !is Application) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (this is FragmentActivity) {
return !this.isDestroyed
} else if (this is Activity) {
return !this.isDestroyed
}
}
}
return true
}
Run Code Online (Sandbox Code Playgroud)
Glide.with(getApplicationContext())
除非您真的需要,否则请不要使用,原因在这里讨论:
Glide image loading with application context
此处概述了正确答案:https : //github.com/bumptech/glide/issues/1484#issuecomment-365625087
在 Kotlin 中,这可以写成一个扩展函数:
fun Context.isValidGlideContext() = this !is Activity || (!this.isDestroyed && !this.isFinishing)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25103 次 |
最近记录: |