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

时间:2012-09-18 08:59:35

标签: java android android-listview nullpointerexception android-ui

我得到一个奇怪的NullPointerException。我的代码没有指向。另外我知道我的应用程序仅在以下情况下提供此NullPointerException:

制造商:索尼爱立信
产品:MT11i_1256-3856
Android版本:2.3.4

有什么想法吗?

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)

1 个答案:

答案 0 :(得分:8)

我的应用程序中有很多类似的例外。我做了一些关于Android操作系统源代码的研究,得出了一个结论 - 这是Android OS Gingerbread及其下面的错误,它已经在冰淇淋三明治中得到修复。

如果您想了解更多详情,请查看Gingerbread源代码树中方法AbsListView.contentFits的源代码:

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

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

很明显,如果调用空列表,此方法将抛出NullPointerException,因为getChildAt(0)将返回NULL。这已在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;
}

如您所见,检查(childCount == 0)

关于此问题的解决方法 - 您可以使用try-catch声明自己的课程MyListView extends ListView,覆盖方法onTouchEvent和对super.onTouchEvent()的环绕声调用块。当然,您需要在应用程序的所有位置使用自定义ListView类。