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.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 說到無聊經濟,首先我們要先來解釋一下,什么是無聊經濟。 無聊經濟是在平凡的世界當中,我們創造出來的一種消磨時間的一...
    夢鹿是一只貓閱讀 640評論 0 1
  • 這一天,好混亂。 寫不完的作業,忙不完的事情。 心情過山車般上上下下。為自己的專業能力擔憂。為自己的語言能力擔憂。...
    莎倫小姐閱讀 125評論 0 0
  • 4.26《極簡生活 簡而美的活》 【day45盈盈】 身邊的小確幸,當我們做自己想做的事,買自己真正需要的東西時,...
    蘇小盈閱讀 118評論 0 0
  • (一) 一個有個性的女孩 初識小夏時, 看到一個 低頭 含羞 淺笑 的女孩在那靜靜的坐著。 那天陽光明媚, 如女孩...
    瀟湘雨妃閱讀 656評論 0 1