我得到了一个奇怪的NullPointerException
。我的代码中没有指向。我还知道,我的应用程序只在以下几个方面提供了这个NullPointerException:
制造商:索尼爱立信
产品: MT11i_1256-3856
安卓-版本: 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)
发布于 2012-10-23 13:28:12
我在我们的应用程序中有很多类似的例外。我在Android操作系统的源代码中做了一些研究,并得出了一个结论--这是Android操作系统姜饼和更低版本的bug,它已经在冰激凌三明治中修复了。
如果需要更多详细信息,请查看姜饼源代码树中的方法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)
支票。
对于解决此问题的解决方案,您可以声明您自己的类MyListView extends ListView
,重写方法onTouchEvent
,并使用try-catch块包围对super.onTouchEvent()
的调用。当然,您需要在应用程序的所有位置使用您的自定义ListView类。
https://stackoverflow.com/questions/12473625
复制相似问题