Android RecyclerView:尝试调用虚拟方法' android.view.ViewGroupgetLayoutParams()'在null对象引用上

时间:2015-05-28 14:37:57

标签: android android-recyclerview

我在RecyclerView中使用CardView。我正在使用该线程来更新CardView中的值。 当我每100毫秒更新一次值。更新CardView后,我将删除CardView。删除应用程序崩溃后出错。

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:12729)
        at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS.java:35)
        at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:940)
        at android.support.v4.view.ViewCompat.canScrollVertically(ViewCompat.java:1237)
        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:5420)
        at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:5293)
        at android.view.View$AccessibilityDelegate.sendAccessibilityEventUnchecked(View.java:20728)
        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:5278)
        at android.view.ViewRootImpl$SendWindowContentChangedAccessibilityEvent.run(ViewRootImpl.java:6939)
        at android.view.ViewRootImpl$SendWindowContentChangedAccessibilityEvent.runOrPost(ViewRootImpl.java:6965)
        at android.view.ViewRootImpl.postSendWindowContentChangedCallback(ViewRootImpl.java:6156)
        at android.view.ViewRootImpl.notifySubtreeAccessibilityStateChanged(ViewRootImpl.java:6269)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2790)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2790)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2790)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2790)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2790)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2790)
        at android.view.View.notifySubtreeAccessibilityStateChangedIfNeeded(View.java:7983)
        at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4186)
        at android.view.View

删除项目时出现此错误。 要删除的代码如下:

    private void removeItem(int position){
    if (mDatasets.size() > position) {
        mDatasets.remove(position);
//            notifyItemRemoved(position);
            notifyDataSetChanged();
        }
        else {
            Log.e(TAG, "Index out of range : " + mDatasets.size() + " " + position);
            notifyDataSetChanged();
        }
    }

此外,只有在我使用

时才会出现此错误
notifyItemRemoved(position)

和Moto X.这适用于Nexus 5.

任何想法如何优雅地处理这个?

1 个答案:

答案 0 :(得分:-1)

我遇到了类似的错误。 我的建议是" 检查空", 像这样的东西:

if(dataset!=null){
remove(x);    
    }

希望这有帮助。