我创建了一个扩展KeyEvent的类:
public class myKeyEvent extends KeyEvent {
public static final int MY_KEYCODE_01 = KeyEvent.KEYCODE_A;
//...
public static final int MY_KEYCODE_30 = KeyEvent.KEYCODE_Z;
}
现在,我想从另一个类(另一个文件)获取变量名称的Integer值(例如“MY_KEYCODE_01”应该返回整数值KeyEvent.KeyCODE_A).
我试过了:
try{
Class cls = myKeyEvent.class.getClass();
Field field = cls.getDeclaredField("MY_KEYCODE_01");
int value = (Integer) field.get(cls);
Log.v("TAG", "Field value is " + value);
} catch (NoSuchFieldException e) {
Log.e("TAG", "Field either doesn't exist or is not public: " + e.toString() );
}
在LogCat中:
Field either doesn't exist or is not public: java.lang.NoSuchFieldException: MY_KEYCODE_01
我该怎么做?