android setTag (int key, Object tag)抛出IllegalArgumentException的问题
2015-07-13 09:14
489 查看
android setTag (int key, Object tag)抛出IllegalArgumentException的问题
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变量和硬编码一个值的方式都是行不通的。
如果只需要设置一个tag,那么直接调用setTag(Object tag)方法就可以轻松搞定,如果一定需要使用多个tag绑定,那么需要先在res/values/strings.xml中添加
使用的时候写成
imageView.setTag(R.id.tag_first, “Hello”);
imageView.setTag(R.id.tag_second, “Success”);
就可以了
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变量和硬编码一个值的方式都是行不通的。
如果只需要设置一个tag,那么直接调用setTag(Object tag)方法就可以轻松搞定,如果一定需要使用多个tag绑定,那么需要先在res/values/strings.xml中添加
[code]<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”);
就可以了
相关文章推荐
- 黑马程序员_JAVA之API(Object,Scanner,数组高级,包装等类)
- Objective-c开发教程--如何在项目中使用AFNetworking
- GObject对象系统
- 盲人摸象——IOS简单应用ToDoList(Objective-c)
- Objective-c开发教程--MRC和ARC混编
- 如何解决CRITICAL glance [-] AttributeError: 'NoneType' object has no attribute 'drivername'
- 记得有一个奇怪的ORA-04028: cannot generate diana for object
- Objective-C(十九、通知-消息发送模式之一)——iOS开发基础
- Objective-C 中 nil/NULL/NSNull
- Objective-C 僵尸对象
- 编写高质量的Objective-C代码
- Cannot find interface declarartion for 'nsobject' & Unknow type name 'NSString'
- The Truth About .NET Objects And Sharing Them Between AppDomains
- OC中的错误arc forbids explicit message send of
- PageObject
- id、NSObject、id<NSObject>三者的区别
- mysql_fetch_row,mysql_fetch_array,mysql_fetch_object,mysql_fetch_assoc的区别!
- Objective-C语法之代码块(block)的使用
- Objective-C内存管理
- IOS学习笔记01-关于objective-c的成员属性