为什么我的应用程序在片段内使用回收器视图时崩溃?
Why is my app crashing when using a recycler view inside a fragment?
片段代码:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ListingsActivity extends Fragment {
RecyclerView recyclerView;
LinearLayoutManager layoutManager;
List<ModelClass> itemList = new ArrayList<>();
Adapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Initialize recycler view
initData();
initRecyclerView();
return inflater.inflate(R.layout.activity_listings, container, false);
}
// Add and remove data
private void initData() {
itemList.add(new ModelClass(R.drawable.placeholder2, "Bottle","Gently used bottle with replaceable nib", "", "Abdallah", "24/05/2002",R.drawable.seperator_line));
itemList.add(new ModelClass(R.drawable.placeholder2, "Bottle","Gently used bottle with replaceable nib", "", "Abdallah", "24/05/2002", R.drawable.seperator_line));
itemList.add(new ModelClass(R.drawable.placeholder2, "Bottle","Gently used bottle with replaceable nib", "", "Abdallah", "24/05/2002", R.drawable.seperator_line));
}
// Initialize recyclerView function
private void initRecyclerView() {
recyclerView = (RecyclerView) getView().findViewById(R.id.listings_container);
layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new Adapter(itemList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
适配器代码:
package com.jamdev.handmedown;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<ModelClass> itemList = new ArrayList<>();
public Adapter(List<ModelClass> itemList){
this.itemList = itemList;
}
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listing_design, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
int resource = itemList.get(position).getListing_pic();
String title = itemList.get(position).getListing_title();
String description = itemList.get(position).getListing_description();
String price = itemList.get(position).getListing_price();
String owner = itemList.get(position).getListing_owner();
String date = itemList.get(position).getListing_date();
int divider = itemList.get(position).getSeperator();
holder.setData(resource,title,description,price,owner,date,divider);
}
@Override
public int getItemCount() {
return itemList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView listing_pic;
private TextView listing_title;
private TextView listing_description;
private TextView listing_price;
private TextView listing_owner;
private TextView listing_date;
private ImageView seperator;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listing_pic = itemView.findViewById(R.id.listing_pic);
listing_title = itemView.findViewById(R.id.listing_title);
listing_description = itemView.findViewById(R.id.listing_description);
listing_price = itemView.findViewById(R.id.listing_price);
listing_owner = itemView.findViewById(R.id.listing_owner);
listing_date = itemView.findViewById(R.id.listing_date);
seperator = itemView.findViewById(R.id.seperator);
}
public void setData(int resource, String title, String description, String price, String owner, String date, int divider) {
listing_pic.setImageResource(resource);
listing_title.setText(title);
listing_description.setText(description);
listing_price.setText(price);
listing_owner.setText(owner);
listing_date.setText(date);
seperator.setImageResource(divider);
}
}
}
注释掉 initRecyclerView() 不会使其崩溃,单独打开布局而不注释掉 initRecyclerView 而不使用片段不会使其崩溃,但不会显示任何项目。所以我有以下问题 1. 与片段一起使用时崩溃 2. 未在片段中使用时不显示项目
非常感谢您的帮助
您在 initRecyclerView()
中调用 getView()
太早了。
getView()
returns whatever onCreateView()
returned 和你的 onCreateView()
还没有完成。因此 getView()
return 为 null 并尝试对该 return 值调用方法将 NPE。
您可以将 initRecyclerView()
移动到 onViewCreated()
以解决该问题。
片段代码:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ListingsActivity extends Fragment {
RecyclerView recyclerView;
LinearLayoutManager layoutManager;
List<ModelClass> itemList = new ArrayList<>();
Adapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Initialize recycler view
initData();
initRecyclerView();
return inflater.inflate(R.layout.activity_listings, container, false);
}
// Add and remove data
private void initData() {
itemList.add(new ModelClass(R.drawable.placeholder2, "Bottle","Gently used bottle with replaceable nib", "", "Abdallah", "24/05/2002",R.drawable.seperator_line));
itemList.add(new ModelClass(R.drawable.placeholder2, "Bottle","Gently used bottle with replaceable nib", "", "Abdallah", "24/05/2002", R.drawable.seperator_line));
itemList.add(new ModelClass(R.drawable.placeholder2, "Bottle","Gently used bottle with replaceable nib", "", "Abdallah", "24/05/2002", R.drawable.seperator_line));
}
// Initialize recyclerView function
private void initRecyclerView() {
recyclerView = (RecyclerView) getView().findViewById(R.id.listings_container);
layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new Adapter(itemList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
适配器代码:
package com.jamdev.handmedown;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<ModelClass> itemList = new ArrayList<>();
public Adapter(List<ModelClass> itemList){
this.itemList = itemList;
}
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listing_design, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
int resource = itemList.get(position).getListing_pic();
String title = itemList.get(position).getListing_title();
String description = itemList.get(position).getListing_description();
String price = itemList.get(position).getListing_price();
String owner = itemList.get(position).getListing_owner();
String date = itemList.get(position).getListing_date();
int divider = itemList.get(position).getSeperator();
holder.setData(resource,title,description,price,owner,date,divider);
}
@Override
public int getItemCount() {
return itemList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView listing_pic;
private TextView listing_title;
private TextView listing_description;
private TextView listing_price;
private TextView listing_owner;
private TextView listing_date;
private ImageView seperator;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listing_pic = itemView.findViewById(R.id.listing_pic);
listing_title = itemView.findViewById(R.id.listing_title);
listing_description = itemView.findViewById(R.id.listing_description);
listing_price = itemView.findViewById(R.id.listing_price);
listing_owner = itemView.findViewById(R.id.listing_owner);
listing_date = itemView.findViewById(R.id.listing_date);
seperator = itemView.findViewById(R.id.seperator);
}
public void setData(int resource, String title, String description, String price, String owner, String date, int divider) {
listing_pic.setImageResource(resource);
listing_title.setText(title);
listing_description.setText(description);
listing_price.setText(price);
listing_owner.setText(owner);
listing_date.setText(date);
seperator.setImageResource(divider);
}
}
}
注释掉 initRecyclerView() 不会使其崩溃,单独打开布局而不注释掉 initRecyclerView 而不使用片段不会使其崩溃,但不会显示任何项目。所以我有以下问题 1. 与片段一起使用时崩溃 2. 未在片段中使用时不显示项目
非常感谢您的帮助
您在 initRecyclerView()
中调用 getView()
太早了。
getView()
returns whatever onCreateView()
returned 和你的 onCreateView()
还没有完成。因此 getView()
return 为 null 并尝试对该 return 值调用方法将 NPE。
您可以将 initRecyclerView()
移动到 onViewCreated()
以解决该问题。