我很困惑,我需要你的帮助开发人员!
MyApp启动ERROR java.lang.NullPointerException:尝试在null对象上调用接口方法'int java.util.List.size()'引用使用RecyclerView和带有片段的CardView。
@Override
public int getItemCount()
{
//ERROR NullPointerException
return visitaList.size();
}
整个适配器Java代码
公共类VisitaAdapter扩展了RecyclerView.Adapter {
private Context context;
private List<Visita> visitaList;
public VisitaAdapter(Context ctx, List<Visita> visitaList){
this.context = ctx;
this.visitaList= visitaList;
}
@Override
public VisitaViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
//inflating and return our view holder
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.layout_visita, null);
return new VisitaViewHolder(view);
}
@Override
public void onBindViewHolder(VisitaViewHolder holder, int position) {
//getting the product of the specific position
Visita visita =visitaList.get(position);
//binding data with the viewholder
holder.textViewEmpresa.setText(visita.getTitle_empresa());
holder.textViewMision.setText(visita.getMision());
holder.textViewFecha.setText(visita.getFecha());
holder.imageView.setImageDrawable(context.getResources()
.getDrawable(visita.getImage()));
}
@Override
public int getItemCount()
{
//ERROR NullPointerException
return visitaList.size();
}
class VisitaViewHolder extends RecyclerView.ViewHolder {
TextView textViewEmpresa, textViewMision, textViewFecha;
ImageView imageView;
public VisitaViewHolder(View itemView) {
super(itemView);
textViewEmpresa = itemView.findViewById(R.id.textViewEmpresa);
textViewMision = itemView.findViewById(R.id.textViewMision);
textViewFecha = itemView.findViewById(R.id.textViewFecha);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
片段启动recyclerview
public class ReplantFrag extends Fragment {
//Adding to the list all items
List<Visita> visitaList;
//The Recycler view
RecyclerView recyclerView;
public ReplantFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_replanta, container, false);
//getting the recyclerview from xml
recyclerView = (RecyclerView) view.findViewById(R.id.reciclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
VisitaAdapter adapter = new VisitaAdapter(getActivity(), visitaList);
recyclerView.setAdapter(adapter);
//agregar items a la maqueta Recycler+CardView
List<Visita> visitaList = new ArrayList<>();
//adding items in list
visitaList.add(new Visita(
1,
"ARAUCO S.A.",
"Revisión Caldera",
"20-01-2018",
R.drawable.arauco_logo));
visitaList.add(new Visita(
2,
"ARAUCO S.A.",
"Inspeccion Nariz Toro",
"22-01-2018",
R.drawable.arauco_logo));
visitaList.add(new Visita(
3,
"COLBUN S.A.",
"Revisión Tuberias",
"25-01-2018",
R.drawable.colbun_logo));
return view;
}
}
谢谢Mate !!
答案 0 :(得分:0)
首先创建visitaList
对象,然后像这样设置适配器
visitaList = new ArrayList<>();
VisitaAdapter adapter = new VisitaAdapter(getActivity(), visitaList);
recyclerView.setAdapter(adapter);
//adding items in list
visitaList.add(new Visita(
1,
"ARAUCO S.A.",
"Revisión Caldera",
"20-01-2018",
R.drawable.arauco_logo));
visitaList.add(new Visita(
2,
"ARAUCO S.A.",
"Inspeccion Nariz Toro",
"22-01-2018",
R.drawable.arauco_logo));
visitaList.add(new Visita(
3,
"COLBUN S.A.",
"Revisión Tuberias",
"25-01-2018",
R.drawable.colbun_logo));
adapter.notifydatasetchanged();
return view;
答案 1 :(得分:0)
您需要在visitaList
中添加项目后初始化适配器。这应该做得很好。
List<Visita> visitaList = new ArrayList<>();
//adding items in list
visitaList.add(new Visita(
1,
"ARAUCO S.A.",
"Revisión Caldera",
"20-01-2018",
R.drawable.arauco_logo));
visitaList.add(new Visita(
2,
"ARAUCO S.A.",
"Inspeccion Nariz Toro",
"22-01-2018",
R.drawable.arauco_logo));
visitaList.add(new Visita(
3,
"COLBUN S.A.",
"Revisión Tuberias",
"25-01-2018",
R.drawable.colbun_logo));
//Now that your list is setup you wont have a null list going into your adapter.
VisitaAdapter adapter = new VisitaAdapter(getActivity(), visitaList);
recyclerView.setAdapter(adapter);
return view;
答案 2 :(得分:0)
你的问题是你将Null传递给适配器。
此行List<Visita> visitaList ;
被称为声明&#34; visitaList为null&#34;。
此行visitaList = new ArrayList<>();
称为创建。
除非你创建一个对象或为它指定一个引用。它是空的。
所以你有两个选择
1-创建列表并在传递给适配器之前对其进行初始化。
2 - 或者使用new ArrayList<>()
创建它并将其传递给适配器,然后在这种情况下你必须告诉适配器列表有一些变化(这是你的初始化)。所以你打电话给adapter.notifydatasetchanged();