The key must be an application-specific resource id
AndroidIllegalargumentexceptionAndroid Problem Overview
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;
Android Solutions
Solution 1 - Android
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");
Solution 2 - Android
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.
Solution 3 - Android
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)
...
Solution 4 - Android
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);
Solution 5 - Android
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);
}
Solution 6 - Android
I've used viewHolder.itemTitleTextView.getId()
. But you can also declare in your resources:
<item type="id" name="conversation_thread_id"/>
Solution 7 - Android
you can use this :
private static final int TAG_ONLINE_ID = View.generateViewId() + 2 << 24;
for uniqness application-specific resource id
Solution 8 - Android
This works for me:
setTag(0xffffffff,objContact.onlineid);
Solution 9 - Android
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
Solution 10 - Android
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