bugfix> java > 投稿

android studioのデフォルトのリストフラグメントを使用していますが、アクティビティで使用するとクラッシュします。同様のエラーが表示されましたが、有用な修正は見つかりませんでした(アダプター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)

フラグメント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);
        }
    }
}

アクティビティ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();
    }

dumpimContent.ITEMSは、25個の要素「itemi」の単なるリストです。

回答 1 件
  • 修正は、リサイクラービューをフレームレイアウト内に配置することです。

    注意:Activity#onCreateのフラグメントもインフレートしましたが、すでにxmlで自動的にインフレートされていたため、最初のリストの背後に「ファントム」リストが作成されていました。

    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>
    
    

    アクティビティ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;
        }
    
    

あなたの答え