转自: http://blog.csdn.net/brokge/article/details/8536906

setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息,在很多场合下都得到妙用。

setTag(Object tag)方法比较简单,这里主要谈一谈带两个参数的setTag方法。

官方的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变量和硬编码一个值的方式都是行不通的。

privatestaticfinalint TAG_ONLINE_ID =1;
((Button) row.findViewById(R.id.btnPickContact)).setTag(TAG_ONLINE_ID,objContact.onlineid);
05-1820:29:38.044: ERROR/AndroidRuntime(5453): java.lang.IllegalArgumentException:The key must be an application-specific resource id.05-1820:29:38.044: ERROR/AndroidRuntime(5453):     at android.view.View.setTag(View.java:7704)05-1820:29:38.044: ERROR/AndroidRuntime(5453):     at com.mypkg.viewP.inflateRow(viewP.java:518)
如果只需要设置一个tag,那么直接调用setTag(Object tag)方法就可以轻松搞定,如果一定需要使用多个tag绑定,那么需要先在res/values/strings.xml中添加
<resources>
<item type="id" name="tag_first"></item>
<item type="id" name="tag_second"></item>
</resources>
使用的时候写成

imageView.setTag(R.id.tag_first,"Hello");
imageView.setTag(R.id.tag_second,"Success");

就可以了
取值的时候
String tag_first=v.getTag(R.id.tag_first).tostring();
就能取到值了!

[Android] View.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)的更多相关文章

  1. spring boot 配置https 报这个错误:java.lang.IllegalArgumentException: Private key must be accompanied by certificate chain

    找了接近半天的时间,原来是那么小的问题 server.ssl.key-store=test.jksserver.ssl.key-store-password=123456server.ssl.key- ...

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

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

  3. 关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析

    在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log: // CRASH: com.meizu.media.painter (pid 12491) // Short Msg: java. ...

  4. Android java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inn

    AsyncTaskLoader: http://developer.Android.com/intl/zh-CN/reference/android/content/AsyncTaskLoader.h ...

  5. android——fragment长时间home或者锁屏java.lang.IllegalArgumentException:No view found for id for....

    在项目中遇到了这个问题.头痛了很久,总是无法重现,也不能很好的解决.总是在息屏后一段时间,就会报java.lang.IllegalArgumentException:No view found for ...

  6. 关于java.lang.IllegalArgumentException: View not attached to window manager 错误的分析

    今天遇到一个很奇特的问题,当用户设置了PIN码,在锁屏界面正常解锁PIN码后,进入Launcher时显示com.android.phone 已停止运行.一开始猜想会不会是解锁PIN码的时候处理导致了P ...

  7. java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@450b2f48 异常处理

    晕死的错误,改了半天也没想到是这样的原因,基础正要呀... 先看一下警告信息: 07-07 08:32:19.540: WARN/WindowManager(74): Failed looking u ...

  8. Android 多点触控错误处理(java.lang.IllegalArgumentException: pointerIndex out of range)

    最近做View的多点触控时,每次第一次触控事件完美运行,第二次就直接崩了,错误信息如下: 01-03 00:05:44.220 4377-4410/system_process E/AndroidRu ...

  9. decorview that was originally added here or java.lang.IllegalArgumentException: View not attached to window manager

    使用Dialog的时候,没少出现下面这两个报错 12-11 17:47:49.776: E/WindowManager(11461): android.view.WindowLeaked: Activ ...

随机推荐

  1. 采访ServiceStack的项目领导Demis Bellot——第1部分(网摘)

    ServiceStack是一个开源的.支持.NET与Mono平台的REST Web Services框架.InfoQ有幸与Demis Bellot深入地讨论了这个项目.在这篇两部分报道的第1部分中,我 ...

  2. Common Issues Which Cause Roles to Recycle

    This section lists some of the common causes of deployment problems, and offers troubleshooting tips ...

  3. 转一篇Unity的相机动画控制

    最近真是忙,连研究细看的时间都没有了,原帖地址:https://alastaira.wordpress.com/2013/11/08/smooth-unity-camera-transitions-w ...

  4. Java 增强型的for循环 for each

    Java 增强型的for循环 for each For-Each循环 For-Each循环也叫增强型的for循环,或者叫foreach循环. For-Each循环是JDK5.0的新特性(其他新特性比如 ...

  5. Http协议中的Content-Length属性

    Android开发的时候需要与从服务器上获取数据,数据是通过http协议封装的.Android端使用的是Xutils第三方插件来发起http请求,但是每次只能拿到部分数据.通过仔细分析后原来是Cont ...

  6. tornado和django的结合使用 tornado Server for django WSGI APP

    #!/usr/bin/env python # Run this with # Serves by default at # http://localhost:8080/hello-tornado a ...

  7. iOS开发小技巧--定时器的使用技巧

    一.定时器的使用技巧 -- 定义好了定时器后,添加两个方法,一个是添加定时器的方法,另一个是移除定时器的方法. 使用的时候也要注意,一定先移除之前的timer,然后再添加timer

  8. zoj3261 并查集离线处理

    Connections in Galaxy War Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  9. Json-遍历

    1.js遍历 var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13} ...

  10. Java中字符串的几个实例

    String str=new String("abc");new 对象时,位于堆中,同时看字符串常量中是否有字符串"abc",如果没有,则进行添加,同时进行关联 ...