键必须是特定于应用程序的资源 ID
为什么我会收到此异常?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您无法使用 setTag(int, Object) 的原因是因为 android 需要在 'int' 参数中预编译唯一 id。
尝试在 String.xml xml 中创建两个唯一的条目,例如“名字”和“名字”。 “名字”&按如下方式使用它们
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
我参加聚会有点晚了,但我今天自己偶然发现了这个问题,并认为我也应该给出答案。这个答案将是其他答案的一些汇编,但有所不同。首先,正如其他人指出的那样,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。 html 和 http://developer.android.com/guide/topics/resources/ more-resources.html 了解更多信息。
我的建议是创建一个名为values/tags.xml 的新文件并写入:
我认为最好创建一个单独的文件,而不是像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:
I think it's better to create a separate file instead of putting it in strings.xml as EtienneSky suggested.
这将完成这项工作...
如果你的类中只有 1 个 setTag,你可以使用任何 int,也许是在顶部声明的 static final。
当您有 2 个或更多带有不同键的 setTag 时,问题就会出现。
我的意思是:
这种情况是错误的。然后,您需要添加一个名为 ids.xml 的值文件,其中包含以下内容:
然后,在您的类中调用:
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:
That scenario is wrong. You then need to add a value file called maybe ids.xml with the following:
Then, in your class, call:
标签id必须是唯一的,因此它希望它是在资源文件中创建的id以保证唯一性。
如果视图只包含一个标签,尽管你可以这样做
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
应该有效。来自 ceph3us 的更多信息:
来自来源:
should work. More info from ceph3us:
from source:
我使用了viewHolder.itemTitleTextView.getId()。但您也可以在资源中声明:
I've used
viewHolder.itemTitleTextView.getId()
. But you can also declare in your resources:<item type="id" name="conversation_thread_id"/>
你可以使用这个:
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
这对我有用:
This works for me:
你想通过 id 保存值的原因是你想在这个标签中覆盖多个值,对吗?
这是一个更简单的解决方案:
假设您想将两个值(字符串)保存到此标记中:“firstname”和“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:
... and access them by splitting them into an string array:
这是一个适合我的简单解决方法:
这里的重要线索是在字符串上调用
.hashCode();
Here is a simple workaround that works for me:
the important clue here is to call
.hashCode();
on the String