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

Glide使用问题

  1. Glide setTag崩溃问题

java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting 
- 原因: 
and an Adapter which is using the usual ViewHolder pattern, i.e. inflating the view from resource (above) and setTag(vh);, now Glide wants to do the same when loading the image. 
- 解决方案: 
There could easily be a way to prevent conflicts, by allowing the ViewTarget users to set an ID to use with setTag(int, Object), even if it’s just a static method on ViewTarget (default beingView.NO_ID -> setTag(Object)).

  1. 如何为Glide设置Tag 
    -> java.lang.IllegalArgumentException: The key must be an application-specific resource id.

    • 原因: 

    官方的api文档中提到:“ The specified key should be an id declared in the resources of the application to ensure it is unique (see the ID resource type). Keys identified as belonging to the Android framework or not associated with any package will cause an IllegalArgumentExceptionto be thrown.”所以抛出IllegalArgumentException的原因就在于key不唯一,那么如何保证这种唯一性呢?定义一个final类型的int变量和硬编码一个值的方式都是行不通的。

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">private static final int TAG_ONLINE_ID = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
((Button) row<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.findViewById</span>(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.btnPickContact</span>))<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTag</span>(TAG_ONLINE_ID,objContact<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.onlineid</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">29</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">38.044</span>: ERROR/AndroidRuntime(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5453</span>): java<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lang</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.IllegalArgumentException</span>: The key must be an application-specific resource id.
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">29</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">38.044</span>: ERROR/AndroidRuntime(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5453</span>):     at android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.view</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.View</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTag</span>(View<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.java</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7704</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">29</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">38.044</span>: ERROR/AndroidRuntime(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5453</span>):     at <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mypkg</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.viewP</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.inflateRow</span>(viewP<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.java</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">518</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>


  • 解决 

setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息,在很多场合下都得到妙用。 
setTag(Object tag)方法比较简单,这里主要谈一谈带两个参数的setTag方法。 
如果只需要设置一个tag,那么直接调用setTag(Object tag)方法就可以轻松搞定,如果一定需要使用多个tag绑定,那么需要先在res/values/strings.xml中添加 




使用的时候写成 
imageView.setTag(R.id.tag_first, “Hello”); 
imageView.setTag(R.id.tag_second, “Success”); 
就可以了 
取值的时候 
String tag_first=v.getTag(R.id.tag_first).tostring(); 
就能取到值了!


转自:http://blog.csdn.net/zhao0829wang/article/details/49277949

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值