Android RecyclerView最后一项删除(运行时)错误

时间:2015-03-24 19:54:43

标签: android runtime-error android-recyclerview

我有RecyclerView个自定义列表项和CardView项,有两个不同的Activities。现在,当我从列表中删除最后一个剩余项目时,我收到错误,我没有更改与acitivitiesadapterRecyclerView相关的任何代码。

错误是:

java.lang.NullPointerException
        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:12745)
        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:5292)
        at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:5160)
        at android.view.View$AccessibilityDelegate.sendAccessibilityEventUnchecked(View.java:20325)
        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:5143)
        at android.view.ViewRootImpl$SendWindowContentChangedAccessibilityEvent.run(ViewRootImpl.java:7653)
        at android.view.ViewRootImpl$SendWindowContentChangedAccessibilityEvent.runOrPost(ViewRootImpl.java:7679)
        at android.view.ViewRootImpl.postSendWindowContentChangedCallback(ViewRootImpl.java:6871)
        at android.view.ViewRootImpl.notifySubtreeAccessibilityStateChanged(ViewRootImpl.java:6944)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.ViewGroup.notifySubtreeAccessibilityStateChanged(ViewGroup.java:2746)
        at android.view.View.notifySubtreeAccessibi

此处的代码没有任何相关内容。我正在使用dependencies

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.android.support:support-v4:21.0.3'

compile 'se.emilsjolander:StickyScrollViewItems:1.+'
compile 'net.i2p.android.ext:floatingactionbutton:1.8.0'
compile 'com.afollestad:material-dialogs:0.6.4.7'}

知道可能存在什么问题吗?

2 个答案:

答案 0 :(得分:7)

我通过为LinearLayoutManager创建子类来解决此问题。这是一个不幸的解决方案,但至少它会工作,直到支持库修复此问题...

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

public class LinearLayoutManagerWrapper extends LinearLayoutManager {
    private static final String TAG = "LinearLayoutManagerWrapper";
    public LinearLayoutManagerWrapper(final Context context) {
        super(context);
    }
    @Override
    public void onInitializeAccessibilityEvent(final AccessibilityEvent event) {
        try {
            super.onInitializeAccessibilityEvent(event);
        } catch (final NullPointerException e) {
            Log.e(TAG, "NullPointerException in onInitializeAccessibilityEvent(" + event +
                    ')', e);
        }
    }
    @Override
    public void onInitializeAccessibilityEvent(final RecyclerView.Recycler recycler,
            final RecyclerView.State state, final AccessibilityEvent event) {
        try {
            super.onInitializeAccessibilityEvent(recycler, state, event);
        } catch (final NullPointerException e) {
            Log.e(TAG, "NullPointerException in onInitializeAccessibilityEvent(" + recycler +
                    ", " + state + ", " + event + ')', e);
        }
    }
}

答案 1 :(得分:1)

发生错误,因为其中一个库使用v7:22appcompat的{​​{1}}库。我通过写:

覆盖了使用v7:21的依赖
recyclerview

我还不确定为什么v7:22最新的库会在回收站查看最后一项删除时出错。