RecyclerView$ViewHolder.shouldIgnore()

NullPointerException - Attempt to invoke virtual method RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

我想给RecyclerView动态的add一个头view,但是报出了这样的错误。

解决方案

I found the source of this problem. Within RecyclerView.dispatchLayoutStep3(), there's a for loop, "for (int i = 0; i < count; ++i)", where count is based on mChildHelper.getChildCount(). While this iteration is occurring, the collection managed by ChildHelper is modified by ChildHelper.hideViewInternal(), which results in null being returned from the call to mChildHelper.getChildAt() on line 3050 of RecyclerView, which in turn results in null being returned from getChildViewHolderInt() on the same line of code (RecyclerView:3050).
Here's the chain of method calls that results in the modification that breaks the integrity of the for loop:
dispatchLayoutStep3() -> animateChange() -> addAnimatingView() -> hide() -> hideViewInternal()
When ChildHelper adds the child param to its mHiddenViews collection, it violates the integrity of the for loop way up in dispatchLayoutStep3().

I see two workarounds for this: 
1>Disable change animation in your RecyclerView 
2>Downgrade to 23.1.1, where this wasn't a problem

看到这就有点懵比了,但是项目需求就是一个列表,索性直接拿ListView来替换RecyclerView.

你可能感兴趣的:(RecyclerView$ViewHolder.shouldIgnore())