gpt4 book ai didi

Java - Class.forName,如何从类中获取字段

转载 作者:行者123 更新时间:2023-11-29 07:37:38 24 4
gpt4 key购买 nike

我正在尝试使用 Class.forName 从 android.os.Build 类获取字符串,但我收到错误 NoSuchFieldException。

这是我的代码:

public String getValue() {
String field = "";
try {
Class b = Class.forName("android.os.Build");
Class build = b.getClass();

Field myField = build.getDeclaredField("UNKNOWN");
field = myField.toString();

} catch (ClassNotFoundException e) {
e.printStackTrace();

} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return field;
}

我想要获取的字符串实际上是“SDK_INT”,它在 VERSION 中,但我想我会先尝试从主类中获取一个字段。这是还包含 Version 类的构建类:

package android.os;

public class Build {
public static final String UNKNOWN = "unknown";
public static final String ID = null;
public static final String DISPLAY = null;
public static final String PRODUCT = null;
public static final String DEVICE = null;
public static final String BOARD = null;
public static final String CPU_ABI = null;
public static final String CPU_ABI2 = null;
public static final String MANUFACTURER = null;
public static final String BRAND = null;
public static final String MODEL = null;
public static final String BOOTLOADER = null;
/** @deprecated */
@Deprecated
public static final String RADIO = null;
public static final String HARDWARE = null;
public static final String SERIAL = null;
public static final String TYPE = null;
public static final String TAGS = null;
public static final String FINGERPRINT = null;
public static final long TIME = 0L;
public static final String USER = null;
public static final String HOST = null;

public Build() {
throw new RuntimeException("Stub!");
}

public static String getRadioVersion() {
throw new RuntimeException("Stub!");
}

public static class VERSION_CODES {
public static final int CUR_DEVELOPMENT = 10000;
public static final int BASE = 1;
public static final int BASE_1_1 = 2;
public static final int CUPCAKE = 3;
public static final int DONUT = 4;
public static final int ECLAIR = 5;
public static final int ECLAIR_0_1 = 6;
public static final int ECLAIR_MR1 = 7;
public static final int FROYO = 8;
public static final int GINGERBREAD = 9;
public static final int GINGERBREAD_MR1 = 10;
public static final int HONEYCOMB = 11;
public static final int HONEYCOMB_MR1 = 12;
public static final int HONEYCOMB_MR2 = 13;
public static final int ICE_CREAM_SANDWICH = 14;
public static final int ICE_CREAM_SANDWICH_MR1 = 15;
public static final int JELLY_BEAN = 16;
public static final int JELLY_BEAN_MR1 = 17;
public static final int JELLY_BEAN_MR2 = 18;
public static final int KITKAT = 19;

public VERSION_CODES() {
throw new RuntimeException("Stub!");
}
}

public static class VERSION {
public static final String INCREMENTAL = null;
public static final String RELEASE = null;
/** @deprecated */
@Deprecated
public static final String SDK = null;
public static final int SDK_INT = 0;
public static final String CODENAME = null;

public VERSION() {
throw new RuntimeException("Stub!");
}
}
}

最佳答案

删除以下行:

Class build = b.getClass();

并获取 Class 变量 b 的字段而不是 build:

Field myField = b.getDeclaredField("UNKNOWN");

否则,您正在查找类 java.lang.Class 中的字段,而不是 android.os.Build

关于Java - Class.forName,如何从类中获取字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33737188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com