且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android的空指针异常由FindViewById

更新时间:2023-11-19 12:22:04

1)检查

holder.headText.setText(rowItem.getHeadText());

正好holder.headText返回null

exactly holder.headText return null

2)检查

if(convertView == null) {}
else {
    holder = (ViewHolder) convertView.getTag();
}

执行的第一家分行。

executed first branch

3)通常我这样做:

// in constructor
inflater = LayoutInflater.from(context); // context from Activity

public View getView(int position, View convertView, ViewGroup parent) {
  if (convertView == null) {
    convertView = inflater.inflate(R.layout.listitem_row, null);
  }

  headText = (TextView) convertView.findViewById(R.id.toptext);
  subText  = (TextView) convertView.findViewById(R.id.bottomtext);