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

发布于 2024-09-02 04:26:23 字数 595 浏览 7 评论 0原文

为什么我会收到此异常?

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)

有问题的行是:

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

我将其定义为:

private static final int TAG_ONLINE_ID = 1;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

暗喜 2024-09-09 04:26:23

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

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

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

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");
也只是曾经 2024-09-09 04:26:23

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

该id必须是预编译的唯一id,就像您在values/strings.xml(即R.string.mystring)中放入的字符串所获得的id一样。请参阅http://developer.android.com/guide/topics/resources/available-resources。 htmlhttp://developer.android.com/guide/topics/resources/ more-resources.html 了解更多信息。

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

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

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

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.

我家小可爱 2024-09-09 04:26:23

这将完成这项工作...

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

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

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

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

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

然后,在您的类中调用:

 ...
 setTag(R.id.resourceDrawable, VALUE_1)
 setTag(R.id.imageURI, VALUE_2)
 ...

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)
 ...
我家小可爱 2024-09-09 04:26:23

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

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

setTag(objContact.onlineid);

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);
始于初秋 2024-09-09 04:26:23
private static final int TAG_ONLINE_ID = 1 + 2 << 24;

应该有效。来自 ceph3us 的更多信息:

指定的key应该是在资源中声明的id
应用程序以确保它是唯一的密钥被识别为属于
Android框架或未关联任何包都会导致
抛出 IllegalArgumentException。

来自来源:

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);
}
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);
}
一绘本一梦想 2024-09-09 04:26:23

我使用了viewHolder.itemTitleTextView.getId()。但您也可以在资源中声明:

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

最冷一天 2024-09-09 04:26:23

你可以使用这个:

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

用于唯一性应用程序特定的资源 ID

you can use this :

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

for uniqness application-specific resource id

巾帼英雄 2024-09-09 04:26:23

这对我有用:

setTag(0xffffffff,objContact.onlineid);

This works for me:

setTag(0xffffffff,objContact.onlineid);
会发光的星星闪亮亮i 2024-09-09 04:26:23

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

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

...并通过将它们拆分为字符串数组来访问它们:

String[] data = v.getTag().toString().split(";");
System.out.println(data[0]) //firstname
System.out.println(data[1]) //lastname

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
夜吻♂芭芘 2024-09-09 04:26:23

这是一个适合我的简单解决方法:

int tagKey = "YourSimpleKey".hashCode();

myView.setTag(tagKey, "MyTagObject");

这里的重要线索是在字符串上调用 .hashCode();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文