我正在尝试创建一个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;
}
答案 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}}