gpt4 book ai didi

android - 有检查null怎么会有NullPointerException?

转载 作者:行者123 更新时间:2023-11-29 19:10:13 26 4
gpt4 key购买 nike

注意:我知道这个问题是重复的,您可能被鼓励投反对票,但是,

  • 它发生在 Android Framework 代码中
  • 即使存在空检查且没有多线程,它也会发生。

我最近在 Playstore 上部署了我的 APK 并收到了极少数用户的 NullPointerException(4):

 Exception java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
android.widget.AdapterView.getPositionForView (AdapterView.java:607)
com.snap.testsnapboard.AlbumSelectActivity$1.onItemClick (AlbumSelectActivity.java:114)
android.widget.AdapterView.performItemClick (AdapterView.java:305)
android.widget.AbsListView.performItemClick (AbsListView.java:1146)
android.widget.AbsListView$PerformClick.run (AbsListView.java:3057)
android.widget.AbsListView.onTouchUp (AbsListView.java:3876)
android.widget.AbsListView.onTouchEvent (AbsListView.java:3641)
android.view.View.dispatchTouchEvent (View.java:8481)
android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2400)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2093)
android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2406)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2107)
android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2406)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2107)
android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2406)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2107)
android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2406)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2107)
com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent (PhoneWindow.java:2369)
com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1719)
android.app.Activity.dispatchTouchEvent (Activity.java:2785)
com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent (PhoneWindow.java:2330)
android.view.View.dispatchPointerEvent (View.java:8676)
android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent (ViewRootImpl.java:4141)
android.view.ViewRootImpl$ViewPostImeInputStage.onProcess (ViewRootImpl.java:4007)
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3562)
android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3615)
android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3581)
android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:3698)
android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3589)
android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:3755)
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3562)
android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3615)
android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3581)
android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3589)
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3562)
android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:5825)
android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:5799)
android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:5770)
android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent (ViewRootImpl.java:5915)
android.view.InputEventReceiver.dispatchInputEvent (InputEventReceiver.java:185)
android.os.MessageQueue.nativePollOnce (MessageQueue.java)
android.os.MessageQueue.next (MessageQueue.java:143)
android.os.Looper.loop (Looper.java:122)
android.app.ActivityThread.main (ActivityThread.java:5254)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:902)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:697)

此异常源自行 while ((v = (View) listItem.getParent()) != null && !v.equals(this)) {

AdapterView.java 中:

/**
* Returns the position within the adapter's data set for the view, where
* view is a an adapter item or a descendant of an adapter item.
* <p>
* <strong>Note:</strong> The result of this method only reflects the
* position of the data bound to <var>view</var> during the most recent
* layout pass. If the adapter's data set has changed without a subsequent
* layout pass, the position returned by this method may not match the
* current position of the data within the adapter.
*
* @param view an adapter item, or a descendant of an adapter item. This
* must be visible in this AdapterView at the time of the call.
* @return the position within the adapter's data set of the view, or
* {@link #INVALID_POSITION} if the view does not correspond to a
* list item (or it is not currently visible)
*/
public int getPositionForView(View view) {
View listItem = view;
try {
View v;
while ((v = (View) listItem.getParent()) != null && !v.equals(this)) {
listItem = v;
}
} catch (ClassCastException e) {
// We made it up to the window without find this list view
return INVALID_POSITION;
}

if (listItem != null) {
// Search the children for the list item
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
if (getChildAt(i).equals(listItem)) {
return mFirstPosition + i;
}
}
}

// Child not found!
return INVALID_POSITION;
}

其他值得一提的事情:

  • 我有一个 ListView ,它通过 addFooterView()
  • 附加了一个页脚 View
  • 我想检查此页脚 View 是否已被单击,但什么都不做,因为此 View 仅包含一些不是通常列表项的图形。
  • 为此,我编写了如下代码:

                // Since we have a Footer view attached to the ListView, the position selected
    // might not always map 1-to-1 with the ArrayList we are using to represent the
    // Album model objects list. Lets check if the Footer view has been selected and
    // in this case, do nothing.

    if(myListView.getPositionForView(loadMoreView) == arg2){
    // Blank return
    return;
    }

这是最终调用AdapterView.java 中方法的代码。现在,当 while 循环中存在 null 检查且所有 UI 处理仅发生在主线程中时,为什么会出现 NullPointerException

最佳答案

我想告诉你一些关于空指针异常的小技巧。

如果 listItem 为 null,它将创建异常...如果 listItem 不为 null 但 listItem.getParent() 为 null,它也会创建异常...或者当您将任何对象转换为另一个对象且该对象为 null ..它抛出异常。

(v = (View) listItem.getParent()) != null && !v.equals(this)

在您的例子中,listItem.getParent() 为空,这就是它抛出异常的原因。

你应该这样做:

if(v != null && listItem != null && listItem.getParent() != null && (v = (View) listItem.getParent()) != null && !v.equals(this) {

}

我希望它不会抛出异常。

关于android - 有检查null怎么会有NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45630567/

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