概述

在使用Glide加载图片时,如果出现“You must not call setTag() on a view Glide is targeting”的错误,八成是在使用ListView的时候出现的。简单来说就是原本想简化布局文件的代码,但是很不幸,这样做却会造成错误

解决方案1

如果出错了,你的item八成是这个样子:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

使用Glide不会出错的item布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>

只要在ImageView的外层再加一层父布局,就不会有问题了(LinearLayout,RelativeLayout等都可以)

原因分析

如果追踪错误来源,会找到这里:

@Override
public Request getRequest() {
Object tag = getTag();
Request request = null;
if (tag != null) {
if (tag instanceof Request) {
request = (Request) tag;
} else {
throw new IllegalArgumentException("You must not call setTag() on a view Glide is targeting");
}
}
return request;
}

ImageView中的Tag需要强转成Request。如果,item中只有ImageView,那么在Adapter中:

convertView.setTag(holder);

这句代码等同于:

imageview.setTag(holder);

这样的话,getTag()的对象就不为Request,从而抛出异常。

那Glide为啥要给ImageView设置Tag呢?原因也很容易想到: 
Glide给ImageView设置Tag的原因是为了防止图片加载错乱

解决方案2

在评论区中,一叶飘舟指出了:使用RecyclerView,可以避免该问题,即使布局文件中的代码为下面的代码,也不会出错

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

结语

  1. 如果使用ListView,只需改变item的布局就可以解决问题,不要太纠结。
  2. 如果想要布局简洁,不用改变布局文件,使用RecyclerView来代替ListView

转载请标明出处:http://blog.csdn.net/qq_26411333/article/details/52034444

You must not call setTag() on a view Glide is targeting的更多相关文章

  1. java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting

    将原有项目图片加载框架picasso改为glide,关于picasso和glide文档就自行查阅相关资料 显示 图片 例子 Glide.with(mContext).load(imageUrl).pl ...

  2. Glide加载图片报错You must not call setTag() on a view Glide is targeting

    报错信息为:You must not call setTag() on a view Glide is targeting 原因就是View使用setTag后导致Glide之前请求的标记被清除,强制转 ...

  3. You must not call setTag() on a view Glide is targeting when use Glide

    以下代码是一个显示图片的RecyclerView 的Adapter用到的,当点击图片,跳到另一个Activity显示大图.RecyclerView 与ListView不同 然而没有setOnClick ...

  4. “You must not call setTag() on a view Glide is targeting” 解决

    报错原因大致是因为Glide加载的iamgeView调用了setTag()方法导致的错误, 因为Glide已经默认为ImageView设置的Tag. 解决办法:自定义一个Application,在里面 ...

  5. ListView或GridView的Adapter使用Glide加载图片异常

    报错信息为:You must not call setTag() on a view Glide is targeting 原因就是View使用setTag后导致Glide之前请求的标记被清除,强制转 ...

  6. 使用Glide以及OkHttp集成

    1.glide的使用: 添加依赖: compile 'com.github.bumptech.glide:glide:3.7.0' 调用代码: ImageView imageView = (Image ...

  7. Glide使用详解(一)

    一. 下载 在build.gradle中添加依赖: compile 'com.github.bumptech.glide:glide:3.7.0' 需要support-v4库的支持,如果你的项目没有s ...

  8. Android常用库源码解析

    图片加载框架比较 共同优点 都对多级缓存.线程池.缓存算法做了处理 自适应程度高,根据系统性能初始化缓存配置.系统信息变更后动态调整策略.比如根据 CPU 核数确定最大并发数,根据可用内存确定内存缓存 ...

  9. 它们的定义Adapterg在etView( )正在使用View.setTag()与不同的是不使用。

    首先看使用Tag案件. @Override public View getView(int position, View view, ViewGroup group) { ViewHolder hol ...

随机推荐

  1. Netty版本升级血泪史之线程篇

    1. 背景 1.1. Netty 3.X系列版本现状 根据对Netty社区部分用户的调查,结合Netty在其它开源项目中的使用情况,我们可以看出目前Netty商用的主流版本集中在3.X和4.X上,其中 ...

  2. Nosql database

    http://kb.cnblogs.com/page/42731/ http://nosql-database.org/ http://blog.jobbole.com/1344/ http://ww ...

  3. 改善 ASP.NET MVC 代码库的 5 点建议

    MVC,建议 刚刚检查完支持工单中的一些代码,笔者想针对 ASP.NET MVC 应用的改进写一些建议.这些内容仍在笔者脑海中,愿与各位一同分享.若你已使用 MVC 一段时间,那么以下内容可能并不新鲜 ...

  4. 如何与 DevOps 为伍?

    DevOps 是一个席卷 IT 界的新术语.但它究竟是什么,南非的公司们如何利用它来加快高品质应用程序的开发速度?国外知名博客作者凯西·吉布森找到了一些答案. 其实 DevOps 这个词已经火了一段时 ...

  5. Technology Trader

    zoj2071:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2071 题意:题意一些零件,每一个零件会有一个花费,然后用这 ...

  6. Count Good Substrings

    Codeforces Round #258 (Div. 2) D:http://codeforces.com/problemset/problem/451/D 题意:给你一个字符串,只有ab组成.相同 ...

  7. 计算字符串的最长回文子串 :Manacher算法介绍

    转自: http://www.open-open.com/lib/view/open1419150233417.html Manacher算法 在介绍算法之前,首先介绍一下什么是回文串,所谓回文串,简 ...

  8. QT5.1.1中MinGW4.8的环境变量配置

    1.右击“我的电脑”图标,在弹出的菜单上选择“属性(R)”菜单项. 2.选择“高级”选项卡.点击“环境变量”按钮. 3.点击“新建(W)”按钮,新建环境变量:MINGW_HOME,变量值为MinGW的 ...

  9. 如何测试 Android 中的定时事件

    测试定时事件不太容易,比如要测试 AlarmManager 中定时明天4点的一个事件,你总不能等到明天4点再看看吧. Roman Nurik 提供了两个用来测试定时事件的命令:adb shell du ...

  10. HDOJ/HDU 1297 Children’s Queue(推导~大数)

    Problem Description There are many students in PHT School. One day, the headmaster whose name is Pig ...