在片段中使用回收器视图时出错
Error while using recycler view inside a fragment
我正在尝试在我的仪表板片段中使用回收器视图,但是当我尝试 运行 应用程序时应用程序崩溃并在 logcat window -> [中显示此错误=23=]尝试在空对象引用上调用虚方法'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)'
这是我的片段代码
package com.example.bookhub;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.Arrays;
import java.util.List;
public class Dashboard_Fragment extends Fragment {
RecyclerView Recycler;
RecyclerView.LayoutManager layoutManager;
Dashboard_Recycler_Adapter recyclerAdapter;
List<String> bookList = Arrays.asList(
"P.S. I love You",
"The Great Gatsby",
"Anna Karenina",
"Madame Bovary",
"War & Peace",
"Middlemarch",
"The Adventures of Huckleberry Finn",
"Moby-Dick",
"The Lord of the Rings");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Recycler = getActivity().findViewById(R.id.Recycler);
layoutManager = new LinearLayoutManager(getActivity());
recyclerAdapter = new Dashboard_Recycler_Adapter((Context) getActivity(),bookList);
Recycler.setLayoutManager(layoutManager);
Recycler.setAdapter(recyclerAdapter);
return inflater.inflate(R.layout.fragment_dashboard, container, false);
}
}
这是适配器的代码
package com.example.bookhub;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Dashboard_Recycler_Adapter extends
RecyclerView.Adapter<Dashboard_Recycler_Adapter.DashboardViewHolder>{
List<String> bookList;
public Dashboard_Recycler_Adapter(Context context, List<String> list)
{
this.bookList = list;
}
@NonNull
@Override
public DashboardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_single_row, parent, false);
return new DashboardViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DashboardViewHolder holder, int position)
{
holder.textView.setText(bookList.get(position));
}
@Override
public int getItemCount()
{
return bookList.size();
}
public static class DashboardViewHolder extends RecyclerView.ViewHolder
{
TextView textView;
public DashboardViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.txtRecyclerRowItem);
}
}
}
这是仪表板片段中回收站视图的XML代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".Dashboard_Fragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="@string/dashboard_fragment"
android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.088"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.075" />
</androidx.constraintlayout.widget.ConstraintLayout>
更改此行
RecyclerView Recycler;
至此
RecyclerView recycler;
还有这一行
Recycler = getActivity().findViewById(R.id.Recycler);
至此
recycler= getActivity().findViewById(R.id.Recycler);
您应该先膨胀视图以获取其 RecyclerView。
import java.util.Arrays;
import java.util.List;
public class Dashboard_Fragment extends Fragment {
RecyclerView recycler;
Dashboard_Recycler_Adapter recyclerAdapter;
List<String> books= Arrays.asList(
"P.S. I love You",
"The Great Gatsby",
"Anna Karenina",
"Madame Bovary",
"War & Peace",
"Middlemarch",
"The Adventures of Huckleberry Finn",
"Moby-Dick",
"The Lord of the Rings");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dashboard, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle
savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recycler = view.findViewById(R.id.Recycler);
recyclerAdapter = new Dashboard_Recycler_Adapter(requiredContext(), books);
recycler.setLayoutManager(new LinearLayoutManager(requiredActivity()));
recycler.setAdapter(recyclerAdapter);
}
}
建议:您应该以驼峰格式命名您的 类(DashboardFragment、DashboardRecyclerAdapter 等)。
在片段中,当您需要上下文或 activity.
时使用 resquiredContext 和 requiredActivity
您的布局还没有膨胀,您之前正在使用它。表示布局创建未完成,您正在使用它之前。在 onViewCreated() 方法中编写代码以获取布局的所有组件。
您将代码放在了错误的 method() 中。从 onCreatview() 中删除以下代码。
Recycler = getActivity().findViewById(R.id.Recycler);
layoutManager = new LinearLayoutManager(getActivity());
recyclerAdapter = new Dashboard_Recycler_Adapter((Context)
getActivity(),bookList);
Recycler.setLayoutManager(layoutManager);
Recycler.setAdapter(recyclerAdapter);
重写片段中的 onViewCreated() 方法并将上面的代码复制并粘贴到其中。
更改后您的片段 class 将类似于下面的代码。
public class Dashboard_Fragment extends Fragment {
public Dashboard_Fragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dashboard,
container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle
savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Recycler = getActivity().findViewById(R.id.Recycler);
layoutManager = new LinearLayoutManager(getActivity());
recyclerAdapter = new Dashboard_Recycler_Adapter((Context)
getActivity(),bookList);
Recycler.setLayoutManager(layoutManager);
Recycler.setAdapter(recyclerAdapter);
}
}
我正在尝试在我的仪表板片段中使用回收器视图,但是当我尝试 运行 应用程序时应用程序崩溃并在 logcat window -> [中显示此错误=23=]尝试在空对象引用上调用虚方法'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)'
这是我的片段代码
package com.example.bookhub;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.Arrays;
import java.util.List;
public class Dashboard_Fragment extends Fragment {
RecyclerView Recycler;
RecyclerView.LayoutManager layoutManager;
Dashboard_Recycler_Adapter recyclerAdapter;
List<String> bookList = Arrays.asList(
"P.S. I love You",
"The Great Gatsby",
"Anna Karenina",
"Madame Bovary",
"War & Peace",
"Middlemarch",
"The Adventures of Huckleberry Finn",
"Moby-Dick",
"The Lord of the Rings");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Recycler = getActivity().findViewById(R.id.Recycler);
layoutManager = new LinearLayoutManager(getActivity());
recyclerAdapter = new Dashboard_Recycler_Adapter((Context) getActivity(),bookList);
Recycler.setLayoutManager(layoutManager);
Recycler.setAdapter(recyclerAdapter);
return inflater.inflate(R.layout.fragment_dashboard, container, false);
}
}
这是适配器的代码
package com.example.bookhub;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Dashboard_Recycler_Adapter extends
RecyclerView.Adapter<Dashboard_Recycler_Adapter.DashboardViewHolder>{
List<String> bookList;
public Dashboard_Recycler_Adapter(Context context, List<String> list)
{
this.bookList = list;
}
@NonNull
@Override
public DashboardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_single_row, parent, false);
return new DashboardViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DashboardViewHolder holder, int position)
{
holder.textView.setText(bookList.get(position));
}
@Override
public int getItemCount()
{
return bookList.size();
}
public static class DashboardViewHolder extends RecyclerView.ViewHolder
{
TextView textView;
public DashboardViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.txtRecyclerRowItem);
}
}
}
这是仪表板片段中回收站视图的XML代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".Dashboard_Fragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="@string/dashboard_fragment"
android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.088"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.075" />
</androidx.constraintlayout.widget.ConstraintLayout>
更改此行
RecyclerView Recycler;
至此
RecyclerView recycler;
还有这一行
Recycler = getActivity().findViewById(R.id.Recycler);
至此
recycler= getActivity().findViewById(R.id.Recycler);
您应该先膨胀视图以获取其 RecyclerView。
import java.util.Arrays;
import java.util.List;
public class Dashboard_Fragment extends Fragment {
RecyclerView recycler;
Dashboard_Recycler_Adapter recyclerAdapter;
List<String> books= Arrays.asList(
"P.S. I love You",
"The Great Gatsby",
"Anna Karenina",
"Madame Bovary",
"War & Peace",
"Middlemarch",
"The Adventures of Huckleberry Finn",
"Moby-Dick",
"The Lord of the Rings");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dashboard, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle
savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recycler = view.findViewById(R.id.Recycler);
recyclerAdapter = new Dashboard_Recycler_Adapter(requiredContext(), books);
recycler.setLayoutManager(new LinearLayoutManager(requiredActivity()));
recycler.setAdapter(recyclerAdapter);
}
}
建议:您应该以驼峰格式命名您的 类(DashboardFragment、DashboardRecyclerAdapter 等)。 在片段中,当您需要上下文或 activity.
时使用 resquiredContext 和 requiredActivity您的布局还没有膨胀,您之前正在使用它。表示布局创建未完成,您正在使用它之前。在 onViewCreated() 方法中编写代码以获取布局的所有组件。
您将代码放在了错误的 method() 中。从 onCreatview() 中删除以下代码。
Recycler = getActivity().findViewById(R.id.Recycler);
layoutManager = new LinearLayoutManager(getActivity());
recyclerAdapter = new Dashboard_Recycler_Adapter((Context)
getActivity(),bookList);
Recycler.setLayoutManager(layoutManager);
Recycler.setAdapter(recyclerAdapter);
重写片段中的 onViewCreated() 方法并将上面的代码复制并粘贴到其中。
更改后您的片段 class 将类似于下面的代码。
public class Dashboard_Fragment extends Fragment {
public Dashboard_Fragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dashboard,
container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle
savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Recycler = getActivity().findViewById(R.id.Recycler);
layoutManager = new LinearLayoutManager(getActivity());
recyclerAdapter = new Dashboard_Recycler_Adapter((Context)
getActivity(),bookList);
Recycler.setLayoutManager(layoutManager);
Recycler.setAdapter(recyclerAdapter);
}
}