Java - Class.forName,如何从Class获取Field

时间:2015-11-16 14:00:20

标签: java class reflection

我试图使用Class.forName从android.os.Build类中获取String,但我收到错误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内部,但我想我会先尝试从主要课程中获取一个字段。这是Build类,它还包含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!");
        }
    }
}

2 个答案:

答案 0 :(得分:4)

删除以下行:

Class build = b.getClass();

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

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

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

答案 1 :(得分:1)

了解如何获取Field的值:

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;
    }

感谢帮助人员:)