The key must be an application-specific resource id

后端 未结 10 1413
时光说笑
时光说笑 2020-11-29 18:11

Why do I get this Exception?

05-18 20:29:38.044: ERROR/AndroidRuntime(5453): java.lang.IllegalArgumentException: The key must be an application-specific reso         


        
相关标签:
10条回答
  • 2020-11-29 18:39

    THIS WILL DO THE JOB...

    If you just have 1 setTag in your class, you could use any int, maybe static final declared in the top.

    The problem comes when you had 2 or more setTag's with different keys. I mean:

    public static final int KEY_1 = 1;
    public static final int KEY_2 = 2;
    ...
    setTag(KEY_1)
    setTag(KEY_2)
    ...
    

    That scenario is wrong. You then need to add a value file called maybe ids.xml with the following:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item type="id" name="resourceDrawable" />
        <item type="id" name="imageURI" />
    </resources>
    

    Then, in your class, call:

     ...
     setTag(R.id.resourceDrawable, KEY_1)
     setTag(R.id.imageURI, KEY_2)
     ...
    
    0 讨论(0)
  • 2020-11-29 18:39

    The reason why you want to save the value by an id is, that you want to cover more than one value in this tag, right?
    Here a more simple solution:
    Let's say you want to save two values (Strings) into this tag: "firstname" and "lastname". You can save them both in one string, separated by semicolon:

    v.setTag(firstname + ";" + lastname);
    

    ... and access them by splitting them into an string array:

    String[] data = v.getTag().toString().split(";");
    System.out.println(data[0]) //firstname
    System.out.println(data[1]) //lastname
    
    0 讨论(0)
  • 2020-11-29 18:39

    Here is a simple workaround that works for me:

    int tagKey = "YourSimpleKey".hashCode();
    
    myView.setTag(tagKey, "MyTagObject");
    

    the important clue here is to call .hashCode(); on the String

    0 讨论(0)
  • 2020-11-29 18:42

    The tag id must be unique so it wants it to be an id created in a resources file to guarantee uniqueness.

    If the view will only contain one tag though you can just do

    setTag(objContact.onlineid);
    
    0 讨论(0)
提交回复
热议问题