150

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 resource id.
05-18 20:29:38.044: ERROR/AndroidRuntime(5453):     at android.view.View.setTag(View.java:7704)
05-18 20:29:38.044: ERROR/AndroidRuntime(5453):     at com.mypkg.viewP.inflateRow(viewP.java:518)

the line in question is:

((Button) row.findViewById(R.id.btnPickContact)).setTag(TAG_ONLINE_ID,objContact.onlineid);

and I have it defined as:

private static final int TAG_ONLINE_ID = 1;
0

10 Answers 10

235

The reason you're not able to use setTag(int, Object) is because android require a pre-compiled unique id in the 'int' argument.

Try creating two unique entry in String.xml xml say, "firstname" & "secondname" & use them as below

imageView.setTag(R.string.firstname, "Abhishek");
imageView.setTag(R.string.lastname, "Gondalia");
9
  • 134
    add this to your strings.xml file: <item type="id" name="TAG_ONLINE_ID"/> and you can use like a regular id resource: R.id.TAG_ONLINE_ID
    – EtienneSky
    Dec 23, 2011 at 8:10
  • 9
    this one should be the answer. its more generic. Jul 12, 2012 at 7:42
  • 2
    @AjithMemana why? When the app is compiled, the strings.xml file assigns a unique int resource id to each element. The value of the string element isn't taken into account.
    – ataulm
    Jun 30, 2013 at 19:59
  • 1
    Thank you, that is so great and helpful ... also if you want to get the tag you need to access strings values like v.getTag(R.string.name)
    – Amt87
    Feb 6, 2014 at 10:42
  • 5
    Better create a filename ids.xml in res/values directory and add there all your unique ids.
    – vovahost
    Apr 16, 2015 at 14:06
159

I'm a little late to the party but I stumbled on this problem myself today and thought I'd give an answer as well. This answer will be a bit of a compilation of the other answers, but with a twist. First of all, the id, as has been pointed out by others, can NOT be a constant defined in your code (such as private static final int MYID = 123) or any other int that you define as a field somewhere.

The id has to be a precompiled unique id, just like the ones you get for strings that you put in values/strings.xml (ie R.string.mystring). Refer to http://developer.android.com/guide/topics/resources/available-resources.html and http://developer.android.com/guide/topics/resources/more-resources.html for more information.

My suggestion is that you create a new file called values/tags.xml and write:

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
      <item name="TAG_ONLINE_ID" type="id"/>
    </resources>

I think it's better to create a separate file instead of putting it in strings.xml as EtienneSky suggested.

5
  • 14
    You can even make it to <item name="TAG_ONLINE_ID" type="id"/> to simplify. Jul 19, 2013 at 9:11
  • This is the real solution; Not the newbie "I don't understand what I'm doing but this hack works"-solution
    – Roel
    Jun 29, 2017 at 11:38
  • 2
    note, to access in code use R.id..., and not R.string.. or R.tags...
    – g2server
    Mar 10, 2018 at 22:16
  • This was useful and here is another post where it shows its usage: stackoverflow.com/questions/28492493/…
    – prasad_
    Apr 4, 2018 at 7:30
  • And make sure you put the file in the values and not in the xml folder. :)
    – Nantoka
    Jun 24, 2019 at 12:23
59

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, VALUE_1)
setTag(KEY_2, VALUE_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, VALUE_1)
 setTag(R.id.imageURI, VALUE_2)
 ...
4
  • best explanation for newcomers Jan 20, 2016 at 18:25
  • 1
    This is the real solution: not the newbie hack fix!
    – Roel
    Jun 29, 2017 at 11:35
  • Why call the value you are storing "KEY_.."? Kind of confuses the explanation.
    – sb4
    Dec 5, 2020 at 0:32
  • @sb4 changed answer to use VALUE_ instead of KEY_ Sep 30, 2021 at 11:35
55

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);
7
  • 4
    i still don't get the pattern. i want to set two tags corresponding to say a first and last name. where do i define the integer IDs for these? May 26, 2011 at 1:04
  • 7
    You can use any resource - even just take a random R.id.FOO where FOO is some id in your project. Aug 31, 2011 at 22:34
  • 21
    That's the best way to get unique tag id ? Really Android ?
    – jimmy0251
    Mar 13, 2015 at 10:24
  • Hey there, what if I don't know how many tags are? because I want to generate views according to some data in model....
    – jsina
    Apr 11, 2016 at 12:03
  • A possible solution to asker's problem, but is not the best solution. Should not be marked as the answer...
    – angryITguy
    Aug 2, 2016 at 3:47
8
private static final int TAG_ONLINE_ID = 1 + 2 << 24;

should work. More info from ceph3us:

The specified key should be an id declared in the resources of the application to ensure it is unique Keys identified as belonging to the Android framework or not associated with any package will cause an IllegalArgumentException to be thrown.

from source:

public void setTag(int key, final Object tag) {
    // If the package id is 0x00 or 0x01, it's either an undefined package
    // or a framework id
    if ((key >>> 24) < 2) {
        throw new IllegalArgumentException("The key must be an application-specific "
                + "resource id.");
    }

    setKeyedTag(key, tag);
}
11
  • Because the ID has to be one generated in your R.java file. Aug 17, 2013 at 14:25
  • No, it does not. It has to be greater than 2 << 24, that's all. Aug 17, 2013 at 20:28
  • Well, the 1 + 2 << 24, 2 + 2 << 24 etc approach actually works. Looks like an irregular fix. I like to know why it is like this? Are there any risks, for instance in future versions of Android etc? It is by far smother to use it defined as a constant? Feb 16, 2016 at 22:48
  • Because it is hardcoded in Android SDK. It won't change ever because otherwise all existing android apps will stop working. Feb 17, 2016 at 7:07
  • 1
    I guess the main problem in this solution is that it does not guarantee uniqness of this key, which means that by adding some library or other code that is using setTag feature this logic can break if generated id will match one got from this solution.
    – EdgarK
    Sep 27, 2016 at 19:30
3

I've used viewHolder.itemTitleTextView.getId(). But you can also declare in your resources: <item type="id" name="conversation_thread_id"/>

2

you can use this :

private static final int TAG_ONLINE_ID = View.generateViewId() + 2 << 24;

for uniqness application-specific resource id

0

This works for me:

setTag(0xffffffff,objContact.onlineid);
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.