使用Glide在RelativeLayout图像中加载一个已销毁的活动时,您无法启动该加载。

82

我正在使用RelativeLayout来设置一张图片。为什么我没有使用ImageView呢?因为在RelativeLayout中,我正在设置图标。

我不知道Glide出了什么问题。我在下面发布了堆栈跟踪和相关代码:

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)

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

 }

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>
14个回答

0

1
这种方式为每个项目视图持有者初始化Glide,我认为对性能不利。 - user924

0

只需使用这个辅助类:

/**
 * @Lukas Niessen
 */
public class GlideUsus {

    public static final String TAG = "GlideUsus";

    public interface Interface {
        void doIfValidContext(RequestManager requestManager);
    }

    public static void execute(Context context, GlideUsus.Interface action) {
        if (isValidContextForGlide(context)) {
            action.doIfValidContext(Glide.with(context));
        } else {
            // nothing
            Log.v(TAG, "Loading image failed");
        }
    }

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

使用方法:

GlideUsus.execute(mContext, (a -> a.load(uri).into(image)));

0

hi in view model for frament The activity context is kept even when you press the back button and exit the program. And go back to the program, you do not need to re-value the context because the context is static

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

2
不不不。您永远不应该将活动存储为静态变量。这会导致内存泄漏。https://android-developers.googleblog.com/2009/01/avoiding-memory-leaks.html - Saurabh
那么我该如何在适配器中设置上下文以使用Glide呢?我的Glide在RecyclerView的适配器中。 - ali moradi

-2
在Activity中,我使用Glide.with(getApplicationContext()),而在Adapter中,我使用Glide.with(myContext.getApplicationContext())
//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);
    }

你不应该在Glide.with中使用应用程序上下文,这里是解释:https://dev59.com/s1wZ5IYBdhLWcg3wBcIJ#32887693 - Biscuit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接