概述

在使用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. 【解决】Maven myeclipse出现Could not find the main class

    maven报错:maven org/codehaus/plexus/classworlds/launcher/Launcher (Unsupported major.m...) 解决思路如下: 按照以 ...

  2. 【HTTP】全面比较HTTP GET与POST方法

    全面比较HTTP GET与POST方法 1.       基本信息比较 属性 GET方法 POST方法 历史: 通常使用GET方法发送的数据作为URL的一部分将会被浏览器保存起来 使用POST方法提交 ...

  3. 【转】python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)

    特别留意群邮件方式,这是工作中用得多的. 附件,HTML,图片,都需要的. 文件形式的邮件 [python] view plain copy 1.#!/usr/bin/env python3 2.#c ...

  4. [cocos2d]关于CCSprite的若干问题与误区

    文章 [cocos2d] 利用texture atlases生成动画 中介绍了如何生成动画并绑定在CCSprite实例上. 使用该代码遇到了几个问题,值得mark下 问题1.多实例 问题描述: 新建一 ...

  5. c语言运算符号详细说明

    C语言中具有右结合性的运算符包括所有单目运算符以及赋值运算符(=)和条件运算符.其它都是左结合性. 判断表达式计算顺序时,先按优先级高的先计算,优先级低的后计算,当优先级相同时再按结合性,或从左至右顺 ...

  6. Zookeeper、Solr和Tomcat安装配置实践

    Zookeeper.Solr和Tomcat安装配置实践

  7. 终端command总结

    Ctrl+Alt+Delete Ctrl+Shift+Esc Ctrl+D Alt+F4 Shift+F10 win+m Ctrl+Alt+. Ctrl+A Ctrl+S osk taskmgr cm ...

  8. 小试牛刀——python接口测试小框架

    用例设计: 执行用例代码: # -*- coding: UTF-8 -*-import xlrd,logging,urllib,urllib2,json,sysfrom pylsy import py ...

  9. UVaLive6039 Uva1668 Let's Go Green

    一开始考虑所有边都是单独的一条路径 然后尽量多的合并 #include<cstdio> #include<cstring> #include<cstdlib> #i ...

  10. ajaxPro用法

    一.AjaxPro的使用 1.在项目中添加引用,浏览找到AjaxPro.2.dll文件 2.在Web.config中的system.web里面写入以下代码 </configuration> ...