Что значит ошибка и как ее исправить
NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
Вот LogCat
ava.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.asus.testapp.ContentAdapter.onBindViewHolder(ContentAdapter.java:46)
at com.example.asus.testapp.ContentAdapter.onBindViewHolder(ContentAdapter.java:12)
Logcat указывает на строчку 12
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
и строчку 46
CardFeed cardfeed = cardfeedlist.get(position);
Вот весть код адаптера.
public class ContentAdapter extends
RecyclerView.Adapter<ContentAdapter.ViewHolder> {
private List<CardFeed> cardfeedlist;
public ContentAdapter(List<CardFeed> cardfeed) {
this.cardfeedlist = cardfeed;
}
private static final int LENGTH = 18;
public static class ViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView cvUser;
TextView cvLocation;
ImageView cvPhoto;
TextView cvContent;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.card_feed, parent, false));
cvUser = (TextView) itemView.findViewById(R.id.card_user);
cvLocation = (TextView) itemView.findViewById(R.id.card_location);
cvPhoto = (ImageView) itemView.findViewById(R.id.card_image);
cvContent = (TextView) itemView.findViewById(R.id.card_text);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
CardFeed cardfeed = cardfeedlist.get(position);
holder.cvUser.setText(cardfeed.getName());
holder.cvLocation.setText(cardfeed.getLocation());
holder.cvContent.setText(cardfeed.getDescription());
holder.cvPhoto.setImageResource(cardfeed.getPhoto());
}
@Override
public int getItemCount() {
return LENGTH;
}
}
Вот код List с items
public class CardFeed {
private String name;
private String location;
private String description;
private int photo;
public CardFeed(){
}
public CardFeed(String name, String location, String description, int photo) {
this.name = name;
this.location = location;
this.description = description;
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPhoto() {
return photo;
}
public void setPhoto(int photo) {
this.photo = photo;
}
cardfeedlistnullна момент обращения. Значит вы его таким в адаптер и передали. – ЮрийСПб Dec 02 '17 at 18:15