I have some code that runs fine in 2.3 and above. However, in 2.2 I get an NullPointerException when className is getting assigned:
if(field.getType() == java.util.ArrayList.class)
className = ((ParameterizedType)field.getGenericType()).toString();
EDIT
I did some snooping around on the android docs and found that some of the methods are not available until API level 9. However, getType(), getGenericType(), and ParameterizedType are all said to be supported at API level 1. So why doesn’t this code work?
Field docs: http://developer.android.com/reference/java/lang/reflect/Field.html
ParameterizedType docs: http://developer.android.com/reference/java/lang/reflect/ParameterizedType.html
EDIT
I found out that calling field.getGenericType() does not cause the program to crash, but silently throws a com.sun.jdi.InvocationException. It seems like the Type returned from the call to getGenericType() is not valid? Again, this works on 2.3 and above, but why is this exception being thrown and caught on 2.2 and below?
This is a known issue, here is the ticket in the Android bug tracker: Issue 6636: NPE in org.apache.harmony.luni.lang.reflect.ListOfTypes
It was fixed in Android 2.3 but has not been backported to earlier Android releases which is why you see the crash in Android 2.2.
To be more elaborate: if you run this code (an extended version of your own code) on Android 2.2 :
You will get this exception:
because of the above mentioned bug in the Java reflection implementation. On Android 2.3, the bug has been fixed, and you’ll see this in the logcat instead:
The
com.sun.jdi.InvocationExceptionexception your are seeing is unrelated to this as far as I can tell.