gpt4 book ai didi

android - 删除最后一项时 Recyclerview 崩溃。 NullPointerException : android. View .ViewGroup$LayoutParams android.view.View.getLayoutParams()

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

我有一个 recyclerview,它是使用数组列表填充的。当我从列表中删除一个项目时,它工作正常,直到最后一个项目被删除。下面是在我的 recyclerview 适配器中找到的代码,它删除了一个项目。

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

下面是崩溃日志。日志没有将我定向到代码中的指定位置。所以我不确定是什么导致了这个问题。

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

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference at android.support.v7.widget.OrientationHelper$2.getDecoratedStart(OrientationHelper.java:316) at android.support.v7.widget.LinearLayoutManager.findOneVisibleChild(LinearLayoutManager.java:1633) at android.support.v7.widget.LinearLayoutManager.findFirstVisibleChildClosestToStart(LinearLayoutManager.java:1453) at android.support.v7.widget.LinearLayoutManager.computeScrollOffset(LinearLayoutManager.java:997) at android.support.v7.widget.LinearLayoutManager.computeVerticalScrollOffset(LinearLayoutManager.java:969) at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1175) at android.view.View.canScrollVertically(View.java:12835) at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS.java:35) at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:1136) at android.support.v4.view.ViewCompat.canScrollVertically(ViewCompat.java:1528) at android.support.v7.widget.RecyclerView$LayoutManager.onInitializeAccessibilityEvent(RecyclerView.java:6594) at android.support.v7.widget.RecyclerView$LayoutManager.onInitializeAccessibilityEvent(RecyclerView.java:6573) at android.support.v7.widget.LinearLayoutManager.onInitializeAccessibilityEvent(LinearLayoutManager.java:204) at android.support.v7.widget.RecyclerViewAccessibilityDelegate.onInitializeAccessibilityEvent(RecyclerViewAccessibilityDelegate.java:66) at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl$1.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:228) at android.support.v4.view.AccessibilityDelegateCompatJellyBean$1.onInitializeAccessibilityEvent(AccessibilityDelegateCompatJellyBean.java:56) at android.view.View.onInitializeAccessibilityEvent(View.java:5439) at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:5312) at android.view.View$AccessibilityDelegate.sendAccessibilityEventUnchecked(View.java:20823) at android.support.v4.view.AccessibilityDelegateCompatIcs.sendAccessibilityEventUnchecked(AccessibilityDelegateCompatIcs.java:117) at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateIcsImpl.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:210) at android.support.v4.view.AccessibilityDelegateCompat.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:358) at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl$1.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:255) at android.support.v4.view.AccessibilityDelegateCompatJellyBean$1.sendAccessibilityEventUnchecked(AccessibilityDelegateCompatJellyBean.java:82) at android.view.View.sendAccessibilityEventUnchecked(View.java:5297) at android.view.ViewRootImpl$SendWindowContentChangedAccessibilityEvent.run(ViewRootImpl.java:6934) at android.view.ViewRootImpl$SendWindowContentChangedAccessibilityEvent.runOrPost(ViewRootImpl.java:6960) at android.view.ViewRootImpl.postSendWindowContentChangedCallback(ViewRootImpl.java:6161) at android.view.ViewRootImpl.notifySubtreeAccessibilityStateChanged(ViewRootImpl.java:6274) at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772) at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772) at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772) at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772) at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772) at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772) at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2772) at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(

非常感谢任何帮助!

最佳答案

勾选“not null”,应该没问题。像这样:

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

希望这对您有所帮助。

关于android - 删除最后一项时 Recyclerview 崩溃。 NullPointerException : android. View .ViewGroup$LayoutParams android.view.View.getLayoutParams(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30195650/

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