我尝试访问使用RecyclerView显示cardview的片段时,我的应用程序崩溃了。该应用程序适用于单个活动,但无法使用碎片。我收到此错误
尝试调用虚方法'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView $适配器)
请帮忙。 这是Recyclerfragment
public class RecyclerViewActivity extends android.support.v4.app.Fragment {
private List<Person> persons;
private RecyclerView rv;
private Context context;
private RVAdapter adapter;
public RecyclerViewActivity()
{
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout= inflater.inflate(R.layout.fragment_home, container, false);
rv=(RecyclerView)getActivity().findViewById(R.id.rv);
adapter = new RVAdapter(getActivity(),initializeData());
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
rv.setAdapter(adapter);
return layout;
}
private static List<Person> initializeData(){
List<Person> persons = new ArrayList<>();
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.lavery));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.lillie));
return persons;
}
}
这是适配器
Public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
private LayoutInflater inflater;
List<Person> data = Collections.emptyList();
public RVAdapter(Context context,List<Person> data)
{
inflater= LayoutInflater.from(context);
this.data=data;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
//View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
View v= inflater.inflate(R.layout.item, viewGroup,false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(data.get(i).name);
personViewHolder.personAge.setText(data.get(i).age);
personViewHolder.personPhoto.setImageResource(data.get(i).photoId);
}
@Override
public int getItemCount() {
return data.size();
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
personName = (TextView)itemView.findViewById(R.id.person_name);
personAge = (TextView)itemView.findViewById(R.id.person_age);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),personName.getText(),Toast.LENGTH_LONG).show();
}
});
}
}
}
这是我想要添加的人物对象。
class Person {
String name;
String age;
int photoId;
Person(String name, String age, int photoId) {
this.name = name;
this.age = age;
this.photoId = photoId;
}
}
谢谢
答案 0 :(得分:1)
当您inflate
中layout
onCreateView()
个文件时,您应该在findViewById()
返回时使用View
。
考虑到这一点,请尝试切换rv=(RecyclerView)getActivity().findViewById(R.id.rv);
rv=(RecyclerView) layout.findViewById(R.id.rv);
答案 1 :(得分:1)
rv=(RecyclerView)getActivity().findViewById(R.id.rv);
这是你的问题。您在Activity上调用了findViewById(),但该活动在其布局中找不到您的R.id.rv
,并且它返回null。假设R.id.rv
位于R.layout.fragment_home
内,您应该将该行替换为:rv=(RecyclerView) layout.findViewById(R.id.rv);
。