首页 > 解决方案 > Glide clear() 方法无法解决

问题描述

我正在尝试OutOfMemoryException在我的 Android 应用程序中修复 's,在我想写的 recyclerView 中:

@Override
public void onViewRecycled(final ViewHolder viewHolder) {
    Glide.clear(viewHolder.getImageView());
}

但我得到了错误:

错误:找不到符号方法清除(ImageView)

我在用:

implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'

标签: androidandroid-glide

解决方案


根据方法的文档clear

/**
   * Cancel any pending loads Glide may have for the view and free any resources that may have been
   * loaded for the view.
   *
   * <p> Note that this will only work if {@link View#setTag(Object)} is not called on this view
   * outside of Glide. </p>
   *
   * @param view The view to cancel loads and free resources for.
   * @throws IllegalArgumentException if an object other than Glide's metadata is put as the view's
   *                                  tag.
   * @see #clear(Target)
   */
  public void clear(@NonNull View view) {
    clear(new ClearTarget(view));
  }

因此,您OutOfMemoryException将通过明确的方法处理。

并稍微更改您的代码,将上下文传递给 Glide :

Glide.with(viewHolder.getImageView().getContext()).clear(viewHolder.getImageView());

推荐阅读