2016-03-04 42 views
7

有一些開發人員報告說,看到下面的堆棧跟蹤調用虛方法RecyclerView $ ViewHolder.shouldIgnore()」,因爲升級到Android支持23.2.0:NullPointerException異常 - 嘗試在空對象引用

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference 
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2913) 
    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1445) 
    at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:144) 
    at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:282) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:821) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:606) 
    at android.view.Choreographer.doFrame(Choreographer.java:575) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:807) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:6895) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

出現這種情況當啓用RecyclerView的更改動畫並調用相應的RecyclerView.Adapter方法notifyItemInserted(),notifyItemRemoved()等以指示對由適配器管理的列表進行了單獨更改(與批量更改相反,如所示通過notifyDataSetChanged())。

這是由於RecyclerView中的錯誤,還是我們開發人員做錯了什麼?

+1

所報告的問題已被固定在未來支持圖書館。如果您在將來的支持庫中再次遇到此問題,請通過提供包含示例應用程序在內的所有必要信息來提出新問題來通知我們。如果問題仍然存在,請通過Google問題跟蹤器進行舉報,然後重新審覈。 https://issuetracker.google.com/issues/37078411 –

回答

2

這似乎是由於23.2.0中引入的RecyclerView中的一個錯誤。該錯誤報告爲here,我解釋了我認爲導致該錯誤的錯誤comment #5

這裏是我的解釋,這裏複製爲參考歷史的目的和易用性:

我發現這個問題的根源。在 RecyclerView.dispatchLayoutStep3()中,有一個for循環,「for(int i = 0; i < count; ++ i)」,其中計數基於 mChildHelper.getChildCount()。雖然此迭代正在發生,但由ChildHelper管理的 集合被修改爲 ChildHelper.hideViewInternal(),結果爲返回null返回 從調用到 RecyclerView的第3050行的mChildHelper.getChildAt(),後者又導致null在同一行代碼(RecyclerView:3050)上從 getChildViewHolderInt()返回。

這裏的方法的調用鏈的結果在修飾 中斷的for循環的完整性:

dispatchLayoutStep3() - )> animateChange( - > addAnimatingView() - > 隱藏() - > hideViewInternal()

當ChildHelper增加孩子參數去其mHiddenViews收集, 它違反的for循環的完整性 dispatchLayoutStep3一路()。

我看到這兩種解決方法:

1)在你的RecyclerView

2)降級到23.1.1,其中這不是一個問題

+0

感謝您的信息,這個錯誤似乎是在23.2.1固定(至少現在對我來說) –

+2

我在24.2.1版本中面臨這個問題。我正在嘗試以編程方式添加scrollerview。 –

+1

25.0.0與setHasStableIds(true) – iscariot

0

我遇到了禁止改變動畫剛纔這個例外,我通過將framgent更改爲FragmentLayout來修復它。

我的適配器在片段參數中使用了一些數據,而在xml中使用fragment沒有填充數據,所以發生了這個錯誤。

只要在此發佈,也許對某人有用。

2

在我的情況下,錯誤是因爲我設置了新的RecyclerView。LayoutParams進入項目的rootview

然後我意識到RecyclerView項目視圖實際上將他們的ViewHolders存儲在一個自定義的LayoutParams類中。 因此,當我重置LayoutParams ViewHolder引用永遠消失。之後會導致NullPointerException崩潰。

我停止將RecyclerView.LayoutParams設置爲項目rootView後,問題消失了。 :)

所以。停止做,在你的ViewHolder:

RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
itemRoot.setLayoutParams(params); 
0

如果您確實需要修改設計參數,可以你可以使用默認的項目佈局PARAMS這樣的:

ViewGroup.LayoutParams params = itemView.getLayoutParams(); 
    params.height=xx; 
    params.width= xx; 
    params.yyyy = xxx; 
    itemView.setLayoutParams(params); 
相關問題