我已经阅读了一大堆 SO 问题,但我似乎无法找到答案。
我有以下课程:
public class DatabaseStrings {
public static final String domain =
"CREATE TABLE IF NOT EXISTS domain (" +
"_id INT UNSIGNED, " +
"account VARCHAR(20) NOT NULL DEFAULT '', " +
"domain TINYINT(1) DEFAULT 0, " +
"domain_string VARCHAR(20) DEFAULT '', " +
"user_id INT UNSIGNED DEFAULT 0" +
");";
}
在其他地方,我正在尝试访问这些字符串:
for(Field field : DatabaseStrings.class.getDeclaredFields()) {
field.setAccessible(true); // I don't know if this is necessary, as the members are public
System.out.println(field.getName());
System.out.println(field.getType());
String value = (String) field.get(null); // This line throws an IllegalAccessException inside Eclipse.
// Do something with value
}
为什么我会收到 IllegalAccessException?如果我删除 field.get 行,我可以在 LogCat 中看到以下行:
System.out | domain
System.out | class java.lang.String
参考: