zoukankan      html  css  js  c++  java
  • 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 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 IllegalArgumentExceptionto 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
  • 相关阅读:
    HTML特殊字符编码对照表
    在Echarts 柱形图的单击事件中写入自定义的参数
    IIS7.5支持解析读取.json文件数据 -- 问题
    VS SVN
    WebApi 跨域问题解决方案:CORS
    SQL Server2012中的SequenceNumber尝试
    Oracle数据类型与.NET中的对应关系
    MongoDB 学习 --转
    MongoDB 基础
    CSS魔法堂:你真的懂text-align吗?
  • 原文地址:https://www.cnblogs.com/yipu/p/3870916.html
Copyright © 2011-2022 走看看