Nullpointer Exception by FindViewById in Android

浪尽此生 提交于 2019-12-06 14:02:50

1) check in

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

exactly holder.headText return null

2) check in

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

executed first branch

3) usually I do like this:

// 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);

Try to inflate layout without parent

Use this:

convertView = inflater.inflate(layoutResourceId, null);

Instead this:

convertView = inflater.inflate(layoutResourceId, parent, false);

Edit 1

If this not help, try to inflate layout by id in getView()

convertView = inflater.inflate(R.layout.listitem_row, null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!