I get a strange NullPointerException. There is no pointing in my code. Also I know that my app gives this NullPointerException only on:
Manufacturer : Sony Ericsson
Product : MT11i_1256-3856
Android-version : 2.3.4
Any ideas?
java.lang.NullPointerException
at android.widget.AbsListView.contentFits(AbsListView.java:722)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:2430)
at android.widget.ListView.onTouchEvent(ListView.java:3447)
at android.view.View.dispatchTouchEvent(View.java:3952)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:995)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1711)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1145)
at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1695)
at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2217)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1901)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
I have lots of similar exceptions in our apps. I did some research in Android OS source code, and made a conclusion – this is bug of Android OS Gingerbread and below, and it has been fixed in Ice Cream Sandwich.
If you want more details, look at source code of method
AbsListView.contentFitsin Gingerbread source tree:It’s obvious that this method will throw
NullPointerExceptionif called for empty list, becausegetChildAt(0)will return NULL. This was fixed in ICS source treeAs you can see, there is a check for
(childCount == 0).Regarding a workaround for this issue – you can declare your own class
MyListView extends ListView, override methodonTouchEventand surround call tosuper.onTouchEvent()with a try-catch block. Of course you will need to use your custom ListView class in all places in your app.