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

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

Leave a Comment