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

前端 未结 5 2137
耶瑟儿~
耶瑟儿~ 2020-12-11 14:27

I have been googling and searching to resolve this error for some time and I can`t seem to find out why and how to solve it.

I`m using a customAdapter to fill in my

相关标签:
5条回答
  • 2020-12-11 14:41

    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();
             }
    
    0 讨论(0)
  • 2020-12-11 14:43

    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)
    
    0 讨论(0)
  • 2020-12-11 14:52

    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);
    
    0 讨论(0)
  • 2020-12-11 14:53

    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

    0 讨论(0)
  • 2020-12-11 15:01

    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);
    
    0 讨论(0)
提交回复
热议问题