Reputation: 106
I am trying to display JSON data but it's giving null pointer exception. It can't get the context of custom single item that I'm inflating
public class HotelAdapter extends ArrayAdapter{
private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;
public HotelAdapter(@NonNull Context context, int resource, @NonNull
List<HotelModel> objects) {
super(context, resource, objects);
hotelModelList = objects;
this.resource=resource;
layoutInflater =
(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull
ViewGroup parent) {
ViewHolder holder = null;
if (convertView==null){
holder =new ViewHolder();
convertView = layoutInflater.inflate(R.layout.item_hotel,null);
holder.addr =
(TextView)convertView.findViewById(R.id.hotel_add_mini);
holder.categ =
(TextView)convertView.findViewById(R.id.hotel_rating);
holder.name =
(TextView)convertView.findViewById(R.id.hotel_name);
holder.price =
(TextView)convertView.findViewById(R.id.hotel_price);
holder.rating =
(RatingBar)convertView.findViewById(R.id.ratingBar2);
}
else {
holder= (ViewHolder) convertView.getTag();
}
holder.addr.setText(hotelModelList.get(position).getAddr());
holder.categ.setText(hotelModelList.get(position).getCateg());
holder.name.setText(hotelModelList.get(position).getName());
holder.price.setText(Integer.toString(hotelModelList.get(position).getPrice()));
holder.rating.setRating(hotelModelList.get(position).getRating());
return convertView;
}
class ViewHolder{
private ImageView img;
private TextView name;
private TextView addr;
private TextView categ;
private TextView price;
private RatingBar rating;
}
}
It can't get the reference of TextViews from item_hotel.xml layout files. So It's giving null pointer exception. I have checked the List<>, the list is non null so it's just the reference problem. Moreover, in this
holder.addr =
(TextView)convertView.findViewById(R.id.hotel_add_mini);
The (TextView) is showing redundant but it shouldn't have as findViewById returns a view and we have to cast it into textView.
Upvotes: 0
Views: 172
Reputation: 490
You forgot to set the tag in your if condition and your're using tag in else condition that is the problem.
In your if condition add,
convertView.setTag(holder);
Upvotes: 1
Reputation: 713
Basically the problem is in this line
layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
try to use context which you are getting in constructor of your HotelAdapter
layoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
and set tag on convertView
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_hotel, null);
holder = new ViewHolder();
holder.addr = (TextView) convertView.findViewById(R.id.hotel_add_mini);
holder.categ = (TextView) convertView.findViewById(R.id.hotel_rating);
holder.name = (TextView) convertView.findViewById(R.id.hotel_name);
holder.price = (TextView) convertView.findViewById(R.id.hotel_price);
holder.rating = (RatingBar) convertView.findViewById(R.id.ratingBar2);
convertView.setTag(holder);
} else{
holder= (ViewHolder) convertView.getTag();
}
Upvotes: 0
Reputation: 3189
Replace this:
private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;
with:
private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;
private Context context;
then update your constructor and instead of this:
public HotelAdapter(@NonNull Context context, int resource, @NonNull
List<HotelModel> objects)
{
super(context, resource, objects);
hotelModelList = objects;
this.resource=resource;
layoutInflater =
(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
}
Use following:
public HotelAdapter(@NonNull Context context, int resource, @NonNull
List<HotelModel> objects)
{
super(context, resource, objects);
this.hotelModelList = objects;
this.resource=resource;
this.context = context;
}
After that instead of following line:
convertView = layoutInflater.inflate(R.layout.item_hotel,null);
Use following two lines:
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.item_hotel, parent, false);
Remove following line also:
layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
This will solve your issue you need to provide context to your layout inflater I think that might be the problem in your case.
Upvotes: 2
Reputation: 1149
Try to make a constructor and set context also.
public class HotelAdapter extends ArrayAdapter{
Context mcontext;
private List<HotelModel> mhotelModelList;
public HotelAdapter(Context context, private List<HotelModel> hotelModelList;){
super(context, R.layout.item_hotel, hotelModelList);
this.mcontext = context;
this.mhotelModelList = hotelModelList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)
mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_hotel, null);
holder = new ViewHolder();
holder.addr =(TextView)convertView.findViewById(R.id.hotel_add_mini);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
holder.addr.setText(hotelModelList.get(position).getAddr());
return convertView;
}
}
Upvotes: 0