【问题标题】:Class.forname, get result from a variable by running a methodClass.forname,通过运行方法从变量中获取结果
【发布时间】:2015-11-19 07:29:36
【问题描述】:

我正在尝试使用 Class.forname 从 android 设备获取屏幕大小。 我要获取的变量是 widthPixels 和 heightPixels,但我得到了错误

"预期的接收器类型为 android.util.DisplayMetrics,但得到了 java.lang.reflect.Field"

这是我的代码:

public Object field;
    public String getInstanceValue(String classInstance, String fieldName) {
        try {
            Class Build1 = Class.forName(classInstance);
            Field strField = Build1.getDeclaredField(fieldName);
            strField.setAccessible(true);
            field = strField.get(strField);

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

这是类:

package android.util;

public class DisplayMetrics {
    public static final int DENSITY_LOW = 120;
    public static final int DENSITY_MEDIUM = 160;
    public static final int DENSITY_TV = 213;
    public static final int DENSITY_HIGH = 240;
    public static final int DENSITY_XHIGH = 320;
    public static final int DENSITY_400 = 400;
    public static final int DENSITY_XXHIGH = 480;
    public static final int DENSITY_XXXHIGH = 640;
    public static final int DENSITY_DEFAULT = 160;
    public int widthPixels;
    public int heightPixels;
    public float density;
    public int densityDpi;
    public float scaledDensity;
    public float xdpi;
    public float ydpi;

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

    public void setTo(DisplayMetrics o) {
        throw new RuntimeException("Stub!");
    }

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

    public boolean equals(Object o) {
        throw new RuntimeException("Stub!");
    }

    public boolean equals(DisplayMetrics other) {
        throw new RuntimeException("Stub!");
    }

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

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

android.view.Display类也有getMetrics方法,

public void getMetrics(DisplayMetrics outMetrics) {
        throw new RuntimeException("Stub!");
    }

请帮忙:)

【问题讨论】:

标签: java android class


【解决方案1】:

你应该这样做

Class Build1 = classInstance.getClass();
Field strField = Build1.getDeclaredField(fieldName);
//strField.setAccessible(true); No need as all variables are public
field = strField.get(classInstance); //use classInstance instead of strField

你还得把方法改成

public String getInstanceValue(DisplayMetrics  classInstance, String fieldName)

【讨论】:

  • 对不起,这是错误的。虽然命名为“classInstance”,但这个字符串变量显然是用来标识类而不是实例。方法Field.get(Object) 需要为其检索字段的类的实例。不是字符串实例。
  • 我在这里看到很多反对意见,请说明原因
  • @Seelenvirtuose 我还提到了方法签名的更改,显然 String 是不可接受的,而是所需类的实际实例。
  • @AniketThakur 那你不能打电话给Class.forName(classInstance)。整个问题有点奇怪。这个答案没有用。我继续投反对票,因为它仍然是完全错误的!
  • 好的,所以使用实例本身获取类classInstance.getClass()。已更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 2015-03-20
  • 2016-12-06
  • 2020-06-04
相关资源
最近更新 更多