我有一个recyclerview,使用arraylist填充。当我从列表中删除项目时,它可以正常工作,直到最后一项被删除。下面是我的recyclerview适配器中的代码,它删除了一个项目。
public void removeItem(int position) {
itemsData.remove(position);
notifyItemRemoved(position);
}
下面是崩溃日志。日志不会将我引导到我的代码中的指定位置。所以我不确定是什么导致了这个问题。
我做了一些测试并记录了位置。如果我在arraylist中添加一个项目并在recyclerview中添加项目的位置notifyItemInserted
,则为0.如果我单击添加的项目,则位置为0,如果我删除该项目,则将其移除0,但在将项目移除为位置0时崩溃了。
java.lang.NullPointerException:尝试调用虚拟方法&android; view.view. $ Home;在null对象引用上 在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.computeScrollOffset(LinearLayoutManager.java:997) 在android.support.v7.widget.LinearLayoutManager.computeVerticalScrollOffset(LinearLayoutManager.java:969) 在android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1175) 在android.view.View.canScrollVertically(View.java:12835) 在android.support.v4.view.ViewCompatICS.canScrollVertically(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.RecyclerView $ LayoutManager.onInitializeAccessibilityEvent(RecyclerView.java:6594) 在android.support.v7.widget.RecyclerView $ LayoutManager.onInitializeAccessibilityEvent(RecyclerView.java:6573) 在android.support.v7.widget.LinearLayoutManager.onInitializeAccessibilityEvent(LinearLayoutManager.java:204) 在android.support.v7.widget.RecyclerViewAccessibilityDelegate.onInitializeAccessibilityEvent(RecyclerViewAccessibilityDelegate.java:66) 在android.support.v4.view.AccessibilityDelegateCompat $ AccessibilityDelegateJellyBeanImpl $ 1.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:228) 在android.support.v4.view.AccessibilityDelegateCompatJellyBean $ 1.onInitializeAccessibilityEvent(AccessibilityDelegateCompatJellyBean.java:56) 在android.view.View.onInitializeAccessibilityEvent(View.java:5439) 在android.view.View.sendAccessibilityEventUncheckedInternal(View.java:5312) 在android.view.View $ AccessibilityDelegate.sendAccessibilityEventUnchecked(View.java:20823) 在android.support.v4.view.AccessibilityDelegateCompatIcs.sendAccessibilityEventUnchecked(AccessibilityDelegateCompatIcs.java:117) 在android.support.v4.view.AccessibilityDelegateCompat $ AccessibilityDelegateIcsImpl.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:210) 在android.support.v4.view.AccessibilityDelegateCompat.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:358) 在android.support.v4.view.AccessibilityDelegateCompat $ AccessibilityDelegateJellyBeanImpl $ 1.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:255) 在android.support.v4.view.AccessibilityDelegateCompatJellyBean $ 1.sendAccessibilityEventUnchecked(AccessibilityDelegateCompatJellyBean.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(
非常感谢任何帮助!
答案 0 :(得分:-1)
检查“非空”,应该没问题。像这样:
public void removeItem(int position) {
if(itemsData != null){
itemsData.remove(position);
notifyItemRemoved(position);
}
}
希望这有帮助。