You must not call setTag() on a view Glide is targeting的解决方案
来源:互联网 发布:大数据 hadoop 编辑:程序博客网 时间:2024/06/03 14:58
概述
在使用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" />
- 1
- 2
- 3
- 4
- 5
使用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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
只要在ImageView的外层再加一层父布局,就不会有问题了(LinearLayout,RelativeLayout等都可以)
原因分析
如果追踪错误来源,会找到这里:
@Overridepublic 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;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
ImageView中的Tag需要强转成Request。如果,item中只有ImageView,那么在Adapter中:
convertView.setTag(holder);
- 1
这句代码等同于:
imageview.setTag(holder);
- 1
这样的话,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
- 2
- 3
- 4
- 5
结语
- 如果使用ListView,只需改变item的布局就可以解决问题,不要太纠结。
- 如果想要布局简洁,不用改变布局文件,使用RecyclerView来代替ListView。
转载请标明出处:http://blog.csdn.net/qq_26411333/article/details/52034444
阅读全文
0 0
- You must not call setTag() on a view Glide is targeting的解决方案
- You must not call setTag() on a view Glide is targeting的解决方案
- Error “You must not call setTag() on a view Glide is targeting” when use Glide
- Glide Exception:"You must not call setTag() on a view Glide is targeting"
- Glide异常:java.lang.IllegaliArgumentException:You must not call setTag() on a view Glide is targeting
- Glide加载图片遇到的错误:You must not call setTag() on a view Glide is targeting
- java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting
- You must not call setTag() on a view Glide is targeting
- java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting
- 关于You must not call setTag() on a view Glide is targeting错误处理
- Gilde刷新图片闪烁和You must not call setTag() on a view Glide is targeting如何解决
- 关于向父view添加相同的view问题!The specified child already has a parent. You must call removeView() on the chil
- You may not call SetData on a resource while it is actively set on the GraphicsDevice. Unset it from
- You must call removeView() on the child'
- $(...).on is not a function 解决方案
- The specified child already has a parent. You must call removeView() on the chil
- The specified child already has a parent. You must call removeView() on the
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- javascript 月份日期 取两位保留
- 欢迎使用CSDN-markdown编辑器
- 基于nodejs的开源博客0.1发布了!!!!
- sql server2014分页存储过程(offset fetch next)
- find查找子串
- You must not call setTag() on a view Glide is targeting的解决方案
- 建设网站需要的Bootstrap介绍与操作
- LeetCode Two Sum
- 15算法课程 2. Add Two Numbers
- Finonacci sequence,斐波那契,经典的兔子繁殖,更新为兔子会死,通项推导,循环,递归,dp实现
- 算法--冒泡排序
- 【csf-201703-1】分蛋糕
- 【Android】TextView设置段落间距
- Python基础 第3课