Android 键必须是特定于应用程序的资源 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2859574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:37:56  来源:igfitidea点击:

The key must be an application-specific resource id

androidillegalargumentexception

提问by Pentium10

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;

采纳答案by Robby Pond

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

标签 id 必须是唯一的,因此它希望它是在资源文件中创建的 id 以保证唯一性。

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

如果视图只包含一个标签,尽管你可以这样做

setTag(objContact.onlineid);

回答by ABDroids

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

您无法使用 setTag(int, Object) 的原因是因为 android 需要在 'int' 参数中使用预编译的唯一 id。

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

尝试在 String.xml xml 中创建两个唯一的条目,例如“firstname”和“secondname”并按如下方式使用它们

imageView.setTag(R.string.firstname, "Abhishek");
imageView.setTag(R.string.lastname, "Gondalia");

回答by britzl

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.

我参加聚会有点晚了,但今天我自己偶然发现了这个问题,并认为我也会给出答案。这个答案将是其他答案的汇编,但有所不同。首先,正如其他人所指出的,id 不能是在您的代码中定义的常量(例如 private static final int MYID = 123)或您在某处定义为字段的任何其他 int。

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.htmland http://developer.android.com/guide/topics/resources/more-resources.htmlfor more information.

id 必须是一个预编译的唯一 id,就像你在 values/strings.xml(即 R.string.mystring)中放入的字符串一样。有关详细信息,请参阅http://developer.android.com/guide/topics/resources/available-resources.htmlhttp://developer.android.com/guide/topics/resources/more-resources.html

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

我的建议是创建一个名为 values/tags.xml 的新文件并写入:

    <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.

我认为最好创建一个单独的文件,而不是像 EtienneSky 建议的那样将它放在 strings.xml 中。

回答by Sterling Diaz

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.

如果您的班级中只有 1 个 setTag,则可以使用任何 int,也许是在顶部声明的 static final。

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

当您有 2 个或更多带有不同键的 setTag 时,问题就出现了。我的意思是:

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:

这种情景是错误的。然后,您需要添加一个名为 ids.xml 的值文件,其中包含以下内容:

<?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)
 ...

回答by Anton Duzenko

private static final int TAG_ONLINE_ID = 1 + 2 << 24;

should work. More info from ceph3us:

应该管用。来自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.

指定的键应该是在应用程序资源中声明的 id,以确保它是唯一的键,被标识为属于 Android 框架或不与任何包关联将导致抛出 IllegalArgumentException。

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);
}

回答by ViliusK

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

我用过viewHolder.itemTitleTextView.getId()。但是你也可以在你的资源中声明: <item type="id" name="conversation_thread_id"/>

回答by yu xiaofei

This works for me:

这对我有用:

setTag(0xffffffff,objContact.onlineid);

回答by Johannes Schuh

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:

你想通过一个 id 保存值的原因是,你想在这个标签中覆盖多个值,对吧?
这里有一个更简单的解决方案:
假设您想将两个值(字符串)保存到此标签中:“名字”和“姓氏”。您可以将它们保存在一个字符串中,用分号分隔:

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

回答by Hymanie Cheng

you can use this :

你可以使用这个:

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

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

for uniqness application-specific resource id

对于 uniqness 应用程序特定的资源 ID