我正在使用 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)
CreateView 上的片段:
@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);
}
}
}
创建活动:
@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();
}
dummyContent.ITEMS 只是 25 个元素“item i”的列表。
修复方法是将回收器视图放入框架布局中。
注意:我还在 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>
CreateView 上的活动
@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;
}
此异常可能是由您的layout.xml中的错误引起的