Android: listview: custom items: nullpointerexception, findviewbyid returns null

拥有回忆 提交于 2019-11-28 13:13:54

Search for the views in the convertView that you inflate(and not in the current Activity layout like you currently do):

 mHolder.textViewCenter = (TextView) 
                convertView.findViewById(R.id.textview_list_item_central);

You are inflating the layout, but not using it while fetching it's views

Do this:

 mHolder.textViewCenter = (TextView)convertView.findViewById(R.id.textview_list_item_central)

TextView initialization is wrong

if(convertView == null){
             LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView = inflater.inflate(layoutResourceId, parent, false);
             mHolder = new ViewHolder();
             ****** RETURNS NULL DURING DEBUGGING ******
             mHolder.textViewCenter = (TextView) convertView .findViewById(R.id.textview_list_item_central); //Change done
             convertView.setTag(mHolder);
         }else{
             mHolder = (ViewHolder) convertView.getTag();
         }

change the line

mHolder.textViewCenter = (TextView)findViewById(R.id.textview_list_item_central);

which is below * RETURNS NULL DURING DEBUGGING * by

mHolder.textViewCenter = (TextView) convertView.findViewById(R.id.textview_list_item_central);
sush

try replacing this:

convertView = inflater.inflate(R.layout.listitemview, null);  

where listitemview is your xml in which you defined your ImageView and TextViews..
hope this helps you

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