Recyclerview在删除最后一个项目时崩溃.NullPointerException:android.view.V



我有一个recyclerview,它是使用arraylist填充的。当我从列表中删除一个项目时,它运行良好,直到最后一个项目被删除。下面是在我的recyclerview适配器中找到的代码,它删除了一个项目。

public void removeItem(int position) {
itemsData.remove(position);
notifyItemRemoved(position);
}

下面是崩溃日志。日志不会将我引导到代码中的指定位置。所以我不确定是什么原因造成的问题。

我做了一些测试并记录了这个位置。如果我将一个项目添加到arraylist中,并且在recyclerview中添加notifyItemInserted,则添加的项目的位置为0。如果我点击添加的项目,位置为0,如果我删除项目,我会在位置0删除它,但当删除位置0的项目时,它崩溃了。

java.lang.NullPointerException:尝试在null对象引用上调用虚拟方法"android.view.ViewGroup$LayoutParams android.vview.view.getLayoutParams()"在android.support.v7.widget.OrientationHelper$2.getDecoratedStart(OrientationHelper.java:316)位于android.support.v7.widget.LinearLayoutManager.findOneVisibleChild(LinearLayoutManager.java:1633)位于android.support.v7.widget.LinearLayoutManager.findFirstVisibleChildClosestToStart(LinearLayoutManager.java:1453)位于android.support.v7.widget.LinearLayoutManager.comuteScrollOffset(LinearLayoutManager.java:997)位于android.support.v7.widget.LinearLayoutManager.comuteVerticalScrollOffset(LinearLayoutManager.java:969)在android.support.v7.widget.CyclerView.computeVerticalScrollOffset(RecyclerView.java:1175)在android.view.view.canScrollVertically(view.java:12835)在android.support.v4.view.ViewCompatICS.canVertically上滚动(ViewCompatICS.java:35)在android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:1136)在android.support.v4.view.ViewCompat.canScrollVertically上(ViewCompat.java:1528)位于android.support.v7.widget.CyclerView$LayoutManager.oInitializeAccessibilityEvent(RecyclerView.java:6594)位于android.support.v7.widget.CyclerView$LayoutManager.oInitializeAccessibilityEvent(RecyclerView.java:6573)位于android.support.v7.widget.LinearLayoutManager.onitializeAccessibilityEvent(LinearLayoutManager.java:204)位于android.support.v7.widget.CyclerViewAccessibilityDelegate.onitializeAccessibility Event(RecyclerViewAccess Delegate.java:66)在android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDeegateJellyBeanImpl$1.onitializeAccessibility Event(AccessibilityDelegateCompat.java:228)在android.support.v4.view.AccessibilityDelegateCompatJellyBean$1.onitializeAccessibilityEvent(Accessibility DelegatecompatJellybean.java:56)在android.view.view.oInitializeAccessibilityEvent(view.java:5439)在android.view.view.sendAccessibilityEventUncheckedInternal(view.java:5312)在android.view.view$AccessibilityDelegate.sendAccessibility EventUnchecked(view.java:20823)在android.support.v4.view.AccessibilityDelegateCompatIcs.sendAccessibilityEventUnchecked(Accessibility DelegatecompatICs.java:117)在android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDeegateIcsImpl.sendAccessibility EventUnchecked(Accessibility Delegatecompat.java:210)在android.support.v4.view.AccessibilityDelegateCompat.sendAccessibilityEventUnchecked(Accessibility Delegatecompat.java:358)在android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDeegateJellyBeanImpl$1.sendAccessibilities EventUnchecked(Accessibility Delegatecompat.java:255)在android.support.v4.view.AccessibilityDelegateCompatJellyBean$1.sendAccessibilityEventUnchecked(Accessibility DelegatecompatJellybean.java:82)在android.view.view.sendAccessibilityEventUnchecked(view.java:5297)在android.view.ViewRootImpl$SendWindowContentChangedAccessibilityEvent.run(ViewRootImpl.java:6934)在android.view.ViewRootImpl$SendWindowContentChangedAccessibilityEvent.runOrPost(ViewRootImpl.java:6960)在android.view.ViewRootImpl.postSendWindowContentChangedCallback(ViewRootImpl.java:6161)在android.view.ViewRootImpl.notifySubtreeAccessibilityStateChanged(ViewRootImpl.java:6274)在android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772)在android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772)在android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772)在android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772)在android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772)在android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772)在android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772)在android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(

非常感谢您的帮助,谢谢!

检查"not null"应该没问题。类似这样的东西:

public void removeItem(int position) {
if(itemsData != null){
itemsData.remove(position);
notifyItemRemoved(position);
}
}

希望这能有所帮助。

相关内容

  • 没有找到相关文章

最新更新