View.setTag(key,object)異常:The key must be an application-specific resource id.

 

07-11 13:43:26.184: E/AndroidRuntime(10229): FATAL EXCEPTION: main
07-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 morehtml

緣由分析:java

setTag是android的view類中頗有用的一個方法,能夠用它來給空間附加一些信息,在不少場合下都獲得妙用。android

setTag(Object tag)方法比較簡單,這裏主要談一談帶兩個參數的setTag方法。windows

官方的api文檔中提到: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.」app

因此拋出IllegalArgumentException的緣由就在於key不惟一,那麼如何保證這種惟一性呢?定義一個final類型的int變量和硬編碼一個值的方式都是行不通的。ide

若是隻須要設置一個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>oop

 
 
使用的時候寫成
 
 
imageView.setTag(R.id.tag_first, "Hello");
imageView.setTag(R.id.tag_second, "Success");
 
 
就能夠了
 
參考:http://blog.csdn.net/furongkang/article/details/7267317
相關文章
相關標籤/搜索