NPE,每次我尝试删除ListView中的第一个可见项

时间:2013-08-11 02:30:19

标签: android eclipse listview android-listview

每次我尝试删除listView中的第一项(可见)时,我需要帮助弄清楚为什么我会得到Null指针异常。删除其他所有项目工作正常,没有任何错误,但第一项仍然失败。

这是代码:

private void deleteCurrentItem() {
        ContextualUndoView contextualUndoView = (ContextualUndoView) mDismissView;
        int position = getAbsListView().getPositionForView(contextualUndoView);


        mDeleteItemCallback.deleteItem(position);

    }

详细代码:http://paste.org/66633

错误日志:

08-10 19:09:22.107: E/AndroidRuntime(973): FATAL EXCEPTION: main
08-10 19:09:22.107: E/AndroidRuntime(973): java.lang.NullPointerException
08-10 19:09:22.107: E/AndroidRuntime(973):  at android.widget.AdapterView.getPositionForView(AdapterView.java:597)
08-10 19:09:22.107: E/AndroidRuntime(973):  at com.haarman.listviewanimations.itemmanipulation.contextualundo.ContextualUndoAdapter$RemoveViewAnimatorListenerAdapter.deleteCurrentItem(ContextualUndoAdapter.java:205)
08-10 19:09:22.107: E/AndroidRuntime(973):  at com.haarman.listviewanimations.itemmanipulation.contextualundo.ContextualUndoAdapter$RemoveViewAnimatorListenerAdapter.onAnimationEnd(ContextualUndoAdapter.java:193)
08-10 19:09:22.107: E/AndroidRuntime(973):  at com.nineoldandroids.animation.ValueAnimator.endAnimation(ValueAnimator.java:1034)
08-10 19:09:22.107: E/AndroidRuntime(973):  at com.nineoldandroids.animation.ValueAnimator.cancel(ValueAnimator.java:968)
08-10 19:09:22.107: E/AndroidRuntime(973):  at com.haarman.listviewanimations.itemmanipulation.contextualundo.ContextualUndoAdapter$RecycleViewListener.onMovedToScrapHeap(ContextualUndoAdapter.java:276)
08-10 19:09:22.107: E/AndroidRuntime(973):  at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6449)
08-10 19:09:22.107: E/AndroidRuntime(973):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1256)

1 个答案:

答案 0 :(得分:0)

这一行:

ContextualUndoView contextualUndoView = (ContextualUndoView) mDismissView;

声明你的视图,但没有实例化它(所以它仍然是null)。

在下一行

int position = getAbsListView().getPositionForView(contextualUndoView);

您尝试使用您的视图,但它仍为null,因此您将获得一个空指针。