我在适配器类中的getView()中的TextView抛出空指针异常

时间:2015-04-27 21:21:27

标签: android listview textview listadapter

我正在尝试创建一个Android应用程序,在其中我尝试动态地设置textText()。但它会抛出空指针异常

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

    // LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    Log.i("&&&&& convert view is null", "&&&&& convert view is null");
    LayoutInflater inflater = activity.getLayoutInflater();

    if(convertView == null){
        convertView = inflater.inflate(R.layout.searchlist_repeat_items, parent, false);
        Log.i("@@@@@ convert view is null", "@@@@@ convert view is null");
        holder1 = new ViewHolder1();
        holder1.companyName_textView = (TextView) convertView.findViewById(R.id.companyName_textView);
    }else{
        holder1 = (ViewHolder1) convertView.getTag();
    }

     holder1.companyName_textView.setText("Checking text");

    return convertView;

}
static class ViewHolder1 {
    TextView companyName_textView;
}

1 个答案:

答案 0 :(得分:3)

你错过了if子句中的convertView.setTag(holder1)。当convertView不为空时,您正在调用convertView.getTag(),而null正在返回if(convertView == null){ convertView = inflater.inflate(R.layout.searchlist_repeat_items, parent, false); Log.i("@@@@@ convert view is null", "@@@@@ convert view is null"); holder1 = new ViewHolder1(); holder1.companyName_textView = (TextView) convertView.findViewById(R.id.companyName_textView); convertView.setTag(holder1); }

{{1}}