Pio*_*rew 7 java android nullpointerexception android-ui android-listview
我觉得奇怪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)
Run Code Online (Sandbox Code Playgroud)
我的应用程序中有很多类似的例外.我做了一些关于Android OS源代码的研究,得出了一个结论 - 这是Android OS Gingerbread及其下面的错误,它已经在Ice Cream Sandwich中得到修复.
如果您想了解更多细节,请查看AbsListView.contentFitsGingerbread源代码树中方法的源代码:
private boolean contentFits() {
final int childCount = getChildCount();
if (childCount != mItemCount) {
return false;
}
return getChildAt(0).getTop() >= 0 && getChildAt(childCount - 1).getBottom() <= mBottom;
}
Run Code Online (Sandbox Code Playgroud)
很明显,NullPointerException如果调用空列表,此方法将抛出,因为getChildAt(0)将返回NULL.这已在ICS源代码树中修复
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;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,有一个检查(childCount == 0).
关于此问题的解决方法 - 您可以使用try-catch块声明自己的类MyListView extends ListView,覆盖方法onTouchEvent和环绕调用super.onTouchEvent().当然,您需要在应用程序的所有位置使用自定义ListView类.