Pen*_*m10 142 android illegalargumentexception
为什么我会得到这个例外?
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)
Run Code Online (Sandbox Code Playgroud)
有问题的一行是:
((Button) row.findViewById(R.id.btnPickContact)).setTag(TAG_ONLINE_ID,objContact.onlineid);
Run Code Online (Sandbox Code Playgroud)
我把它定义为:
private static final int TAG_ONLINE_ID = 1;
Run Code Online (Sandbox Code Playgroud)
ABD*_*ids 222
你无法使用setTag(int,Object)的原因是因为android需要在'int'参数中预编译唯一id.
尝试在String.xml xml中创建两个唯一条目,例如"firstname"和"secondname"并使用它们,如下所示
imageView.setTag(R.string.firstname, "Abhishek");
imageView.setTag(R.string.lastname, "Gondalia");
Run Code Online (Sandbox Code Playgroud)
bri*_*tzl 143
我参加派对有点晚了,但我今天自己偶然发现了这个问题并认为我也会给出答案.这个答案将是对其他答案的汇编,但有一点扭曲.首先,正如其他人所指出的那样,id不能是代码中定义的常量(例如private static final int MYID = 123)或任何其他在某处定义为字段的int.
id必须是一个预编译的唯一id,就像你在values/strings.xml(即R.string.mystring)中输入的字符串一样.有关详细信息,请参阅http://developer.android.com/guide/topics/resources/available-resources.html和http://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>
Run Code Online (Sandbox Code Playgroud)
我认为最好创建一个单独的文件,而不是像EtienneSky建议的那样将它放在strings.xml中.
Rob*_*ond 54
标记ID必须是唯一的,因此它希望它是在资源文件中创建的id以保证唯一性.
如果视图只包含一个标记,您可以这样做
setTag(objContact.onlineid);
Run Code Online (Sandbox Code Playgroud)
Ste*_*iaz 52
这将是工作......
如果你的类中只有1个setTag,你可以使用任何int,也就是在top中声明的static final.
当你有2个或更多setTag与不同的键时,问题就出现了.我的意思是:
public static final int KEY_1 = 1;
public static final int KEY_2 = 2;
...
setTag(KEY_1)
setTag(KEY_2)
...
Run Code Online (Sandbox Code Playgroud)
那种情况是错误的.然后,您需要添加名为maybe ids.xml的值文件,其中包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="resourceDrawable" />
<item type="id" name="imageURI" />
</resources>
Run Code Online (Sandbox Code Playgroud)
然后,在你的课堂上,致电:
...
setTag(R.id.resourceDrawable, KEY_1)
setTag(R.id.imageURI, KEY_2)
...
Run Code Online (Sandbox Code Playgroud)
private static final int TAG_ONLINE_ID = 1 + 2 << 24;
Run Code Online (Sandbox Code Playgroud)
应该管用.来自ceph3us的更多信息:
指定的键应该是在应用程序的资源中声明的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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57149 次 |
| 最近记录: |