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 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 ID resource type). Keys identified as belonging to the Android framework or not associated with any package will cause an IllegalArgumentException
to be 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://blog.csdn.net/furongkang/article/details/7267317