Skip to content Skip to sidebar Skip to footer

Nullpointerexception - Attempt To Invoke Virtual Method Recyclerview$viewholder.shouldignore()' On A Null Object Reference

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 'boole

Solution 1:

In my case the error caused because I was setting a new RecyclerView.LayoutParams into the rootview of an item.

Then I realized that RecyclerView item views actually store their ViewHolders in a custom LayoutParams class. So when I reset the LayoutParams ViewHolder reference is gone forever. Which causes a NullPointerException crash later.

The problem is gone after I stopped setting the RecyclerView.LayoutParams into the item rootView. :)

So. Stop doing that in your ViewHolder:

//DON'T DO THAT
RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
itemRoot.setLayoutParams(params);

Solution 2:

If you absolutely need to modify layout params, you can use the default item layout params like this:

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

Solution 3:

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:

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

Solution 4:

I met up with this exception just now, and I fixed it by changing framgent to FragmentLayout.

My adapter used some data in fragment argument, and the using fragment in xml does not fill the data, so the bug occurs.

Just post this here, maybe useful to someone.

Solution 5:

I also encountered into this issue, even with version of RecyclerView 27.1.1. And I had the following code in my project:

    recyclerView.setLayoutManager(mLayoutManager);
    SimpleItemAnimatoritemAnimator=newDefaultItemAnimator();
    itemAnimator.setSupportsChangeAnimations(false);
    recyclerView.setItemAnimator(itemAnimator);
    LayoutInflaterinflater= LayoutInflater.from(getContext());

And I've fixed it after removing addition of Animator to RecyclerView, i.e. this code started to look the following way:

    recyclerView.setLayoutManager(mLayoutManager);
    LayoutInflater inflater = LayoutInflater.from(getContext());

Post a Comment for "Nullpointerexception - Attempt To Invoke Virtual Method Recyclerview$viewholder.shouldignore()' On A Null Object Reference"