我想在android中获取类的字段名称。为此,我使用此代码
public static String checkForNull(Object obj) {
Class cls = obj.getClass();
Field fields[] = cls.getDeclaredFields();
for (Field field : fields) {
Object object = null;
try {
object = field.get(obj);
} catch (IllegalAccessException e) {
return "The field " + field.getName() + " of " + cls.getSimpleName() + " is not accessible!";
}
if (object == null) {
return "The field " + field.getName() + " of " + cls.getSimpleName() + " is null!";
} else if (object instanceof String) {
String string = (String) object;
if (string.equals("")) {
return "The field " + field.getName() + " of " + cls.getSimpleName() + " is empty!";
}
}
}
return null;
}
但是这段代码返回类字段的通用名称,如a,b,c ......等。这段代码在netbeans中运行良好。
答案 0 :(得分:1)
看起来像混淆的名称,很可能你有ProGuard的默认设置,Android SDK会执行一些代码混淆,请参阅https://developer.android.com/studio/build/shrink-code.html
如果您想查看原始字段名称,则应完全禁用混淆或为此特定类/包禁用混淆。