View.setTag(key,object)异常:The key must be an application-specific resource id.
2016-10-09 14:30
495 查看
View.setTag(key,object)异常:The key must be an application-specific resource id.
07-11 13:43:26.184: E/AndroidRuntime(10229): FATAL EXCEPTION: main07-11 13:43:26.184: E/AndroidRuntime(10229): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}: java.lang.IllegalArgumentException: The key must be an application-specific resource id.07-11 13:43:26.184: E/AndroidRuntime(10229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.app.ActivityThread.access$600(ActivityThread.java:128)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.os.Handler.dispatchMessage(Handler.java:99)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.os.Looper.loop(Looper.java:137)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.app.ActivityThread.main(ActivityThread.java:4517)07-11 13:43:26.184: E/AndroidRuntime(10229): at java.lang.reflect.Method.invokeNative(Native Method)07-11 13:43:26.184: E/AndroidRuntime(10229): at java.lang.reflect.Method.invoke(Method.java:511)07-11 13:43:26.184: E/AndroidRuntime(10229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)07-11 13:43:26.184: E/AndroidRuntime(10229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)07-11 13:43:26.184: E/AndroidRuntime(10229): at dalvik.system.NativeStart.main(Native Method)07-11 13:43:26.184: E/AndroidRuntime(10229): Caused by: java.lang.IllegalArgumentException: The key must be an application-specific resource id.07-11 13:43:26.184: E/AndroidRuntime(10229): at android.view.View.setTag(View.java:12585)07-11 13:43:26.184: E/AndroidRuntime(10229): at com.txrj.sms.activity.CreateMessageActivity.getRecipientItem(CreateMessageActivity.java:47)07-11 13:43:26.184: E/AndroidRuntime(10229): at com.txrj.sms.activity.CreateMessageActivity.onCreate(CreateMessageActivity.java:41)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.app.Activity.performCreate(Activity.java:4470)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)07-11 13:43:26.184: E/AndroidRuntime(10229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)07-11 13:43:26.184: E/AndroidRuntime(10229): ... 11 more原因分析: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 IDresource type). Keys identified as belonging to the Android framework or not associated with any package will cause anIllegalArgumentExceptiontobe thrown.”所以抛出IllegalArgumentException的原因就在于key不唯一,那么如何保证这种唯一性呢?定义一个final类型的int变量和硬编码一个值的方式都是行不通的。
如果只需要设置一个tag,那么直接调用setTag(Object tag)方法就可以轻松搞定,如果一定需要使用多个tag绑定,那么需要先在res/values/ids.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");
就可以了
转自:http://www.cnblogs.com/fengzhblog/archive/2013/07/11/3183832.html
相关文章推荐
- View.setTag(key,object)异常 The key must be an application-specific resource id.
- View.setTag(key,object)异常:The key must be an application-specific resource id.
- View.setTag(key,object)异常:The key must be an application-specific resource id.
- Android解决:View.setTag(key,object)异常:The key must be an application-specific resource id.
- View.setTag(key,object)异常:The key must be an application-specific resource id.
- View.setTag(key,object)异常:The key must be an application-specific resource id.
- View.setTag(key,object)异常:The key must be an application-specific resource id.
- [Android] View.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)
- android setTag的妙用和The key must be an application-specific resource id 异常
- Android开发 -- setTag的妙用和The key must be an application-specific resource id 异常
- Android -- setTag的妙用和The key must be an application-specific resource id 异常
- setTag使用错误The key must be an application-specific resource id.
- AndroidRuntime: FATAThe key must be an application-specific resource id.
- The field 'key' must be an object, but got undefined
- Q: ArrayAdapter requires the resource ID to be a TextView
- ArrayAdapter requires the resource ID to be a TextView问题
- ArrayAdapter requires the resource ID to be a TextView问题
- ArrayAdapter requires the resource ID to be a TextView问题
- If you object has an "Id' property, it will be set with the generated Id from MongoDB.
- ArrayAdapter requires the resource id to be a TextView