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



我正在尝试使用Class.forName从android.os.Build class获取字符串,但我收到错误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();

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

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

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

了解如何获取字段的值:

Object field;
    public Object getValue() {
        try {
            Class Build1 = Class.forName("android.os.Build$VERSION");
            Field strField = Build1.getDeclaredField("SDK_INT");
            field = strField.get(strField);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return field;
    }

感谢您的帮助:)

相关内容

  • 没有找到相关文章