NullPointerException - 尝试对空对象引用调用虚拟方法 RecyclerView$ViewHolder.shouldIgnore()' [英] NullPointerException - Attempt to invoke virtual method RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

查看:28
本文介绍了NullPointerException - 尝试对空对象引用调用虚拟方法 RecyclerView$ViewHolder.shouldIgnore()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自升级到 Android Support 23.2.0 以来,一些开发者报告看到以下堆栈跟踪:

Several developers have reported seeing the following stack trace since upgrading to Android Support 23.2.0:

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() 所示).

This occurs when RecyclerView's change animation is enabled and corresponding RecyclerView.Adapter methods notifyItemInserted(), notifyItemRemoved(), etc., are called to indicate that an isolated change was made to the list managed by the adapter (as opposed to a wholesale change, as indicated by notifyDataSetChanged()).

这是由于 RecyclerView 中的错误,还是我们的开发人员做错了什么?

Is this due to a bug in RecyclerView, or are we developers doing something wrong?

推荐答案

这似乎是由于 RecyclerView 中的错误所致,该错误是在 23.2.0 中引入的.该错误已在此处报告,我解释了我的想法导致该错误的评论 #5 中出现错误.

This appears to be due to a bug in RecyclerView, which was introduced in 23.2.0. The bug was reported here, and I explained what I think is causing the error in comment #5 on that bug.

这是我的解释,为了历史目的和方便参考,复制到这里:

Here's my explanation, copied here for historical purposes and ease of reference:

我找到了这个问题的根源.在里面RecyclerView.dispatchLayoutStep3(),有一个for循环,for(int i =0;我<数数;++i)",其中计数基于mChildHelper.getChildCount().在进行此迭代时,ChildHelper 管理的集合被修改ChildHelper.hideViewInternal(),导致返回null从第 3050 行对 mChildHelper.getChildAt() 的调用RecyclerView,这反过来导致从getChildViewHolderInt() 在同一行代码 (RecyclerView:3050) 上.

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).

这是导致修改的方法调用链破坏 for 循环的完整性:

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()

dispatchLayoutStep3() -> animateChange() -> addAnimatingView() -> hide() -> hideViewInternal()

当 ChildHelper 将 child 参数添加到其 mHiddenViews 集合时,它违反了 for 循环方式的完整性dispatchLayoutStep3().

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) 在 RecyclerView 中禁用更改动画

1) Disable change animation in your RecyclerView

2) 降级到 23.1.1,这不是问题

2) Downgrade to 23.1.1, where this wasn't a problem

这篇关于NullPointerException - 尝试对空对象引用调用虚拟方法 RecyclerView$ViewHolder.shouldIgnore()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆