gpt4 book ai didi

java - 在 android.widget.AbsListView.contentFits(AbsListView.java :722)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:34 25 4
gpt4 key购买 nike

我得到一个奇怪的 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)

最佳答案

我的应用程序中有很多类似的异常。我研究了 Android OS 源代码,并得出结论 - 这是 Android OS Gingerbread 及以下版本的错误,它已在 Ice Cream Sandwich 中修复。

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

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 并环绕调用 super.onTouchEvent() 带有 try-catch block 。当然,您需要在应用的所有位置使用自定义 ListView 类。

关于java - 在 android.widget.AbsListView.contentFits(AbsListView.java :722),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12473625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com