I created a class to extend 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;
}
Now, i want to get the Integer value by the variable name (eg. “MY_KEYCODE_01” should return integer value KeyEvent.KeyCODE_A) from another class (another file).
I tried to:
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() );
}
In LogCat:
Field either doesn't exist or is not public: java.lang.NoSuchFieldException: MY_KEYCODE_01
How can I do it?
The class is
myKeyEvent.classif you domyKeyEvent.class.getClass()you are getting the class of the class object which isClass. Drop the getClass()You should be able to see the class is not correct in a debugger.