(已解决)默认android recyclerview viewholder npe shouldingore()

mrzz3bfm  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(1083)

我使用的是androidstudio的默认列表片段,当我在活动中使用它时,它会崩溃。我看到了类似的错误,但没有找到有用的修复方法(我尝试更改适配器getitemcount和recyclerview layoutmanager)
崩溃日志:

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)

创建视图的片段:

@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;
    }

碎片标识列表:

<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" />

循环水适配器:

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

创建时的活动:

@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();
    }

dummcontent.items只是25个元素“item i”的列表。

s5a0g9ez

s5a0g9ez1#

修复方法是将回收器视图放在框架布局中。
注意:我还在activity#oncreate中扩展了片段,尽管它已经用xml自动扩展了,这导致在第一个片段后面有一个“幻影”列表。
碎片标识列表:

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

活动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;
    }

相关问题