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

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

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

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

创建视图的片段:

  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  3. {
  4. View view = inflater.inflate(R.layout.fragment_identity_list, container, false);
  5. if (view instanceof RecyclerView)
  6. {
  7. RecyclerView recyclerView = (RecyclerView) view;
  8. recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
  9. recyclerView.setAdapter(new IdentityRecyclerViewAdapter(DummyContent.ITEMS, mListener));
  10. }
  11. return view;
  12. }

碎片标识列表:

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

循环水适配器:

  1. public class IdentityRecyclerViewAdapter extends RecyclerView.Adapter<IdentityRecyclerViewAdapter.IdentityViewHolder>
  2. {
  3. private final List<DummyItem> mValues;
  4. private final IdentityListFragmentInteractionListener mListener;
  5. public IdentityRecyclerViewAdapter(List<DummyItem> items, IdentityListFragmentInteractionListener listener)
  6. {
  7. mValues = items;
  8. mListener = listener;
  9. }
  10. @Override
  11. public IdentityViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  12. {
  13. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_identity, parent, false);
  14. return new IdentityViewHolder(view);
  15. }
  16. @Override
  17. public int getItemCount()
  18. {
  19. return mValues.size();
  20. }
  21. public class IdentityViewHolder extends RecyclerView.ViewHolder
  22. {
  23. public final View mView;
  24. public final TextView mIdView;
  25. public final TextView mContentView;
  26. public DummyItem mItem;
  27. public IdentityViewHolder(View view)
  28. {
  29. super(view);
  30. mView = view;
  31. mIdView = view.findViewById(R.id.item_number);
  32. mContentView = view.findViewById(R.id.content);
  33. }
  34. }
  35. }

创建时的活动:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState)
  3. {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_list);
  6. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  7. transaction.add(R.id.identityList, IdentityFragment.newInstance(ListType.ALL), "List");
  8. transaction.commit();
  9. }

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

s5a0g9ez

s5a0g9ez1#

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

  1. <FrameLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".activity.IdentityFragment">
  8. <!--PUT RECYCLER VIEW IN A FRAME LAYOUT-->
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/identityRecycler"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. tools:listitem="@layout/fragment_identity" />
  14. </FrameLayout>

活动oncreateview

  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  3. {
  4. View view = inflater.inflate(R.layout.fragment_identity_list, container, false);
  5. RecyclerView recyclerView = view.findViewById(R.id.identityRecycler);
  6. recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
  7. recyclerView.setAdapter(new IdentityRecyclerViewAdapter(DummyContent.ITEMS, mListener))
  8. return view;
  9. }
展开查看全部

相关问题