在扩展数组适配器时自定义适配器的getView()中的空指针异常

时间:2017-12-19 08:37:56

标签: android nullpointerexception

我正在尝试显示JSON数据,但它给出了空指针异常。它无法获得我正在膨胀的自定义单项的上下文

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;
    }
}

它无法从item_hotel.xml布局文件中获取TextViews的引用。所以它给出了空指针异常。我检查了List&lt;&gt ;,该列表为非null,因此它只是引用问题。而且,在此

holder.addr = 
(TextView)convertView.findViewById(R.id.hotel_add_mini);

(TextView)显示多余,但它不应该具有findViewById返回视图,我们必须将其转换为textView。

4 个答案:

答案 0 :(得分:2)

替换它:

private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;

使用:

private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;
private Context context;

然后更新你的构造函数而不是:

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);
}

使用以下内容:

public HotelAdapter(@NonNull Context context, int resource, @NonNull 
List<HotelModel> objects) 
{
    super(context, resource, objects);
    this.hotelModelList = objects;
    this.resource=resource;
    this.context = context;
}

之后而不是以下行:

convertView = layoutInflater.inflate(R.layout.item_hotel,null);

使用以下两行:

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.item_hotel, parent, false);

还删除以下行:

layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

这将解决您需要为布局inflater提供上下文的问题我认为这可能是您案例中的问题。

答案 1 :(得分:1)

您忘记在if条件中设置标记,并且在其他条件中使用标记是问题。

在if条件中添加,

convertView.setTag(holder);

答案 2 :(得分:0)

尝试制作构造函数并设置上下文。

      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;
     }
   }

答案 3 :(得分:0)

基本上问题出在这一行

layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

尝试使用您在HotelAdapter

的构造函数中获得的上下文
layoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);

并在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();
        }