View.setTag(key,object)异常:The key must be an application-specific resource id.
2014-08-27 14:41
375 查看
07-1113:43:26.184:E/AndroidRuntime(10229):FATALEXCEPTION:main
07-1113:43:26.184:E/AndroidRuntime(10229):java.lang.RuntimeException:UnabletostartactivityComponentInfo{com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}:java.lang.IllegalArgumentException:Thekeymustbeanapplication-specificresourceid.
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.access$600(ActivityThread.java:128)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.os.Handler.dispatchMessage(Handler.java:99)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.os.Looper.loop(Looper.java:137)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.main(ActivityThread.java:4517)
07-1113:43:26.184:E/AndroidRuntime(10229):atjava.lang.reflect.Method.invokeNative(NativeMethod)
07-1113:43:26.184:E/AndroidRuntime(10229):atjava.lang.reflect.Method.invoke(Method.java:511)
07-1113:43:26.184:E/AndroidRuntime(10229):atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-1113:43:26.184:E/AndroidRuntime(10229):atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-1113:43:26.184:E/AndroidRuntime(10229):atdalvik.system.NativeStart.main(NativeMethod)
07-1113:43:26.184:E/AndroidRuntime(10229):
Causedby:java.lang.IllegalArgumentException:Thekeymustbeanapplication-specificresourceid.
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.view.View.setTag(View.java:12585)
07-1113:43:26.184:E/AndroidRuntime(10229):atcom.txrj.sms.activity.CreateMessageActivity.getRecipientItem(CreateMessageActivity.java:47)
07-1113:43:26.184:E/AndroidRuntime(10229):atcom.txrj.sms.activity.CreateMessageActivity.onCreate(CreateMessageActivity.java:41)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.Activity.performCreate(Activity.java:4470)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-1113:43:26.184:E/AndroidRuntime(10229):...11more
原因分析:
setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息,在很多场合下都得到妙用。
setTag(Objecttag)方法比较简单,这里主要谈一谈带两个参数的setTag方法。
官方的api文档中提到:
“Thespecifiedkeyshouldbeaniddeclaredintheresourcesoftheapplicationtoensureitisunique(seethe
IDresourcetype).KeysidentifiedasbelongingtotheAndroidframeworkornotassociatedwithanypackagewillcausean
所以抛出IllegalArgumentException的原因就在于key不唯一,那么如何保证这种唯一性呢?定义一个final类型的int变量和硬编码一个值的方式都是行不通的。
<resources>
<itemtype="id"name="tag_first"></item>
<itemtype="id"name="tag_second"></item>
</resources>
07-1113:43:26.184:E/AndroidRuntime(10229):java.lang.RuntimeException:UnabletostartactivityComponentInfo{com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}:java.lang.IllegalArgumentException:Thekeymustbeanapplication-specificresourceid.
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.access$600(ActivityThread.java:128)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.os.Handler.dispatchMessage(Handler.java:99)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.os.Looper.loop(Looper.java:137)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.main(ActivityThread.java:4517)
07-1113:43:26.184:E/AndroidRuntime(10229):atjava.lang.reflect.Method.invokeNative(NativeMethod)
07-1113:43:26.184:E/AndroidRuntime(10229):atjava.lang.reflect.Method.invoke(Method.java:511)
07-1113:43:26.184:E/AndroidRuntime(10229):atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-1113:43:26.184:E/AndroidRuntime(10229):atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-1113:43:26.184:E/AndroidRuntime(10229):atdalvik.system.NativeStart.main(NativeMethod)
07-1113:43:26.184:E/AndroidRuntime(10229):
Causedby:java.lang.IllegalArgumentException:Thekeymustbeanapplication-specificresourceid.
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.view.View.setTag(View.java:12585)
07-1113:43:26.184:E/AndroidRuntime(10229):atcom.txrj.sms.activity.CreateMessageActivity.getRecipientItem(CreateMessageActivity.java:47)
07-1113:43:26.184:E/AndroidRuntime(10229):atcom.txrj.sms.activity.CreateMessageActivity.onCreate(CreateMessageActivity.java:41)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.Activity.performCreate(Activity.java:4470)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-1113:43:26.184:E/AndroidRuntime(10229):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-1113:43:26.184:E/AndroidRuntime(10229):...11more
原因分析:
setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息,在很多场合下都得到妙用。
setTag(Objecttag)方法比较简单,这里主要谈一谈带两个参数的setTag方法。
官方的api文档中提到:
“Thespecifiedkeyshouldbeaniddeclaredintheresourcesoftheapplicationtoensureitisunique(seethe
IDresourcetype).KeysidentifiedasbelongingtotheAndroidframeworkornotassociatedwithanypackagewillcausean
tobethrown.”IllegalArgumentException
所以抛出
如果只需要设置一个tag,那么直接调用setTag(Objecttag)方法就可以轻松搞定,如果一定需要使用多个tag绑定,那么需要先在res/values/ids.xml中添加
<resources>
<itemtype="id"name="tag_first"></item>
<itemtype="id"name="tag_second"></item>
</resources>
使用的时候写成
imageView.setTag(R.id.tag_first,"Hello"); imageView.setTag(R.id.tag_second,"Success");
就可以了
参考:/article/8852820.html
相关文章推荐
- 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.
- 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
- 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问题
- ArrayAdapter requires the resource ID to be a TextView问题
- ArrayAdapter requires the resource ID to be a TextView问题