Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException at android.widget.AbsListView.contentFits(AbsListView.java:722)

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)
like image 661
Piotr Ślesarew Avatar asked Sep 18 '12 08:09

Piotr Ślesarew


1 Answers

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.contentFits in Gingerbread source tree:

private boolean contentFits() {
    final int childCount = getChildCount();
    if (childCount != mItemCount) {
        return false;
    }

    return getChildAt(0).getTop() >= 0 && getChildAt(childCount - 1).getBottom() <= mBottom;
}

It's obvious that this method will throw NullPointerException if called for empty list, because getChildAt(0) will return NULL. This was fixed in ICS source tree

private boolean contentFits() {
    final int childCount = getChildCount();
    if (childCount == 0) return true;
    if (childCount != mItemCount) return false;

    return getChildAt(0).getTop() >= mListPadding.top &&
            getChildAt(childCount - 1).getBottom() <= getHeight() - mListPadding.bottom;
}

As 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 method onTouchEvent and surround call to super.onTouchEvent() with a try-catch block. Of course you will need to use your custom ListView class in all places in your app.

like image 80
HitOdessit Avatar answered Oct 19 '22 22:10

HitOdessit