我正在尝试在运行时获取所有 ID。我正在使用反射。但我得到了 ClassNotFoundException
。
try {
aClass = classLoader.loadClass(getPackageName()+".R.id");
Field[] ID_Fields = aClass.getFields();
int[] resArray = new int[ID_Fields.length];
for(int i = 0; i < ID_Fields.length; i++) {
try {
resArray[i] = ID_Fields[i].getInt(null);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
最佳答案
id
是 R
的内部类。你应该使用美元符号 $
来访问它
classLoader.loadClass(getPackageName()+".R$id");
你也可以使用静态方法Class.forName
aClass = Class.forName(getPackageName()+".R$id");
来自文档:
Returns the Class object associated with the class or interface with the given string name.
关于java - 在运行时找不到 android R.id 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29917642/