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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在扩展数组适配器时自定义适配器的getView()中的空指针异常相关的知识,希望对你有一定的参考价值。

我试图显示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布局文件中获取TextView的引用。所以它给出了空指针异常。我检查了List <>,列表是非null因此它只是引用问题。而且,在此

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

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

答案

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

在你的if条件中添加,

convertView.setTag(holder);
另一答案

替换这个:

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提供上下文的问题我认为这可能是您案例中的问题。

另一答案

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

      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;
     }
   }
另一答案

基本上问题出在这一行

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

以上是关于在扩展数组适配器时自定义适配器的getView()中的空指针异常的主要内容,如果未能解决你的问题,请参考以下文章

运行时自定义光标适配器错误,非法参数异常

未调用自定义适配器 getview

设置列表项的值时自定义适配器中的空指针异常

多次调用自定义列表视图适配器 getView 方法,并且顺序不一致

自定义列表视图适配器 getView 方法被多次调用,并且顺序不一致

未调用自定义适配器 getView() 方法