javaandroidandroid-recyclerviewfragment

Default android RecyclerView ViewHolder NPE shouldIgnore()


I'm using the default list fragment from android studio and it crashes when I use it in my activity. I saw similar errors but I found no useful fix (I tried changing adapter getItemCount and recyclerView layoutManager)

Crash log:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
        at androidx.recyclerview.widget.RecyclerView.findMinMaxChildLayoutPositions(RecyclerView.java:4311)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4045)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3534)
        at android.view.View.measure(View.java:26415)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7845)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)

Fragment onCreateView:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_identity_list, container, false);

        if (view instanceof RecyclerView)
        {
            RecyclerView recyclerView = (RecyclerView) view;

            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerView.setAdapter(new IdentityRecyclerViewAdapter(DummyContent.ITEMS, mListener)); 
        }

        return view;
    }

fragment_identity_list:

<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/identityRecycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"
    tools:context=".activity.IdentityFragment"
    tools:listitem="@layout/fragment_identity" />

RecyclerViewAdapter:

public class IdentityRecyclerViewAdapter extends RecyclerView.Adapter<IdentityRecyclerViewAdapter.IdentityViewHolder>
{
    private final List<DummyItem> mValues;
    private final IdentityListFragmentInteractionListener mListener;

    public IdentityRecyclerViewAdapter(List<DummyItem> items, IdentityListFragmentInteractionListener listener)
    {
        mValues = items;
        mListener = listener;
    }

    @Override
    public IdentityViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_identity, parent, false);
        return new IdentityViewHolder(view);
    }

    @Override
    public int getItemCount()
    {
        return mValues.size();
    }

    public class IdentityViewHolder extends RecyclerView.ViewHolder
    {
        public final View mView;
        public final TextView mIdView;
        public final TextView mContentView;
        public DummyItem mItem;

        public IdentityViewHolder(View view)
        {
            super(view);
            mView = view;
            mIdView = view.findViewById(R.id.item_number);
            mContentView = view.findViewById(R.id.content);
        }
    }
}

Activity onCreate:

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.identityList, IdentityFragment.newInstance(ListType.ALL), "List");
        transaction.commit();
    }

The dummyContent.ITEMS is just a list of 25 elements "item i".


Solution

  • The fix is putting the recycler view inside a frame layout.

    NB: I also inflated the fragment in Activity#onCreate, although it was already automatically inflated with the xml , which caused a "phantom" list behind the first one.

    fragment_identity_list:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activity.IdentityFragment">
    
        <!--PUT RECYCLER VIEW IN A FRAME LAYOUT-->
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/identityRecycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/fragment_identity" />
    </FrameLayout>
    

    Activity onCreateView

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.inflate(R.layout.fragment_identity_list, container, false);
    
            RecyclerView recyclerView = view.findViewById(R.id.identityRecycler);
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerView.setAdapter(new IdentityRecyclerViewAdapter(DummyContent.ITEMS, mListener))
    
            return view;
        }