您无法使用滑行为relativelayout图像中的已销毁活动启动加载

时间:2016-08-23 06:21:38

标签: android android-glide

我使用relativelayout来设置图像。为什么我没有使用imageview方法,在relativelayout图像中,我正在设置图标。

我不知道滑翔中究竟是什么问题。我已经发布了下面的堆栈跟踪和相关代码:

logcat的:

Handler

TabMorePagesDetailActivity.java:

 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)

layout.xml:

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);

 }

11 个答案:

答案 0 :(得分:85)

使用:

Glide.with(getApplicationContext()).load(...)

代替:

Glide.with(TabMorePagesDetailActivity.this).load(...)

希望它能解决你的问题〜

答案 1 :(得分:8)

受GitHub thread的启发,我在加载任何图像之前都在使用它

final Context  context = getContext();
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;
}

答案 2 :(得分:7)

您可以简单地检查上下文是否被销毁;

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
            }
        }
    }
}

这也可以表示为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
}

答案 3 :(得分:5)

我在几天之前遇到了同样的问题。我已经解决了这个问题,将Application上下文内存代表当前的Class上下文内存。

可能会对你有所帮助: -

使用此代码

 Glide.with(getApplicationContext())
           .load(coverIMGurl)
           .asBitmap()
           .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                                    .into(new SimpleTarget<Bitmap>(500, 500) {....}

即使您遇到此问题,请仔细阅读本文“https://github.com/bumptech/glide/issues/1097

此问题的概述:这是Glide库的问题。

答案 4 :(得分:1)

使用具有正确生命周期的参数设置Glide。例如:在自定义视图中使用Glide.with(this)代替Glide.with(getContext())

答案 5 :(得分:1)

在活动中,我使用 Glide.with(getApplicatonContext()) , 在适配器中,我使用 Glide.with(myContext.getApplicatonContext())

//It works for me fine.

//for adapter

Context myContext;

//also initilize in `oncreateViewHolder`

@Override
    public MessagesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_messages_layout, parent, false);
        myContext = parent.getContext();
        return new MessagesHolder(view);
    }

答案 6 :(得分:1)

这是一个 Kotlin 扩展,它汇集了上述几个答案 - thisthisthis

/**
 * Returns true when [Context] is unavailable or is about to become unavailable
 */
fun Context?.isDoomed(): Boolean = when (this) {
    null -> true
    is Application -> false
    is Activity -> (this.isDestroyed or this.isFinishing)
    else -> false
}

答案 7 :(得分:0)

hi在frament的视图模型中 即使按下返回按钮并退出程序,活动上下文也会保留。 回到程序,您不需要重新评估上下文,因为上下文是静态的

@Override
    public void onBindViewHolder(@NonNull final viewholder viewholder, final int i) {
        final Model_Post model_post = list.get(i);
        Log.e(TAG, "onBindViewHolder: "+model_post.getImageurl());

        Glide.with(MainActivity.activity)
                .load(Uri.parse(model_post.getImageurl()))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(viewholder.itemsPostBinding.imgvItempost);

    }

您必须在主要活动中执行此操作:

public static AppCompatActivity activity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = MainActivity.this;
}

答案 8 :(得分:0)

请使用Glide.with(holder.itemView.getContext())。load ...... 它已解决了我的问题ref https://github.com/bumptech/glide/issues/2690

答案 9 :(得分:0)

您可以使用:Glide.with(getActivity()).load(....)

答案 10 :(得分:-1)

在使用Glide加载图像之前尝试此操作,在我的情况下,mirefer是StorageReference,miimagen是ImageView。我解决了这个问题。我希望它可以帮助你。

if (!this.isFinishing ()) {
                // Load the image using Glide
                Glide.with(YourActivity.this)
                        .using(new FirebaseImageLoader())
                        .load(mirefer)
                        .into(miimagen);
            }