使用BaseAdapter实现自定义列表视图时出错

时间:2016-06-01 09:54:41

标签: java android nullpointerexception

我需要帮助我一直试图找到一种方法来实现自定义列表视图,我收到错误: -

  

java.lang.NullPointerException:尝试调用虚方法'void   android.widget.TextView.setText(java.lang.CharSequence)'为null   对象参考   com.edis.edis.HospitalAdaptor.getView(HospitalAdaptor.java:62)

以下是我创建的对象,它名为Data.java

public class Data {

private double lat, lon;
public String name;
String groupA, groupB, groupAB,groupO;
public int url_image;


public Data(double lat, double lon, int url_image, String name, String groupA, String groupB, String groupAB, String groupO)
{
    this.lat =lat;
    this.lon = lon;
    this.url_image =url_image;
    this.name = name;
    this.groupA =groupA;
    this.groupB =groupB;
    this.groupAB = groupAB;
    this.groupO = groupO;
 }
 }

然后我创建了一个Adapter,我在chakula.setText((hospital.name))的行上得到一个错误(null对象)。

以下是我使用BaseAdaptor的适配器。 的 HospitalsAdaptors.java

    public class HospitalAdaptor extends BaseAdapter{
    Context context;
    List<Data> datas;


    public HospitalAdaptor(Context context, List<Data> datas) {

        this.context = context;
        this.datas = datas;

    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Data hospital = (Data)getItem(position);

        if(convertView == null){

             convertView = LayoutInflater.from(context).inflate(R.layout.fragment_hospitals,parent,false);
            //convertView = LayoutInflater.inflate(R.layout.fragment_hospitals,parent,false);
        }

        TextView chakula = (TextView)convertView.findViewById(R.id.hospitals_name);
        ImageView pilau = (ImageView)convertView.findViewById(R.id.hospitals_image);

        chakula.setText(hospital.name);

        Glide
                .with(context)
                .load(hospital.url_image)
                .centerCrop()
                .crossFade()
                .into(pilau);

        return convertView;
    }
}

3 个答案:

答案 0 :(得分:0)

you have just initialize variable not set value

it should be like this

      public Data(double lat, double lon, int url_image, String name, String groupA, String groupB, String groupAB, String groupO)
    {
        this.lat =lat;
        this.lon = lon;
        this.url_image =url_image;
        this.name = name;
        this.groupA =groupA;
        this.groupB =groupB;
        this.groupAB = groupAB;
        this.groupO = groupO;

setname(this.name)  // same apply to all other
     }

答案 1 :(得分:0)

试试这个

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null){

         convertView = LayoutInflater.from(context).inflate(R.layout.fragment_hospitals,parent,false);
        //convertView = LayoutInflater.inflate(R.layout.fragment_hospitals,parent,false);
    }
    Data hospital = datas.get(position);

    TextView chakula = (TextView)convertView.findViewById(R.id.hospitals_name);
    ImageView pilau = (ImageView)convertView.findViewById(R.id.hospitals_image);

    chakula.setText(hospital.name);

    Glide.with(context)
            .load(hospital.url_image)
            .centerCrop()
            .crossFade()
            .into(pilau);

    return convertView;
}

答案 2 :(得分:0)

替换此方法并在Data类中生成getter和setter。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Data hospital = (Data)datas.get(position);

    if(convertView == null){

         convertView = LayoutInflater.from(context).inflate(R.layout.fragment_hospitals,parent,false);
        //convertView = LayoutInflater.inflate(R.layout.fragment_hospitals,parent,false);
    }

    TextView chakula = (TextView)convertView.findViewById(R.id.hospitals_name);
    ImageView pilau = (ImageView)convertView.findViewById(R.id.hospitals_image);

    chakula.setText(hospital.getName());

    Glide
            .with(context)
            .load(hospital.url_image)
            .centerCrop()
            .crossFade()
            .into(pilau);

    return convertView;
}