关闭

ViewHolder模式获取控件空指针异常,小错误很坑爹

标签: debug调试ViewHolder 空指针
1114人阅读 评论(0) 收藏 举报
分类:

在用ListView做开发的时候需要集成BaseAdapter类,复写其getView方法,这个想必大家都是知道的。今天在调试的时候偏偏让我遇到一个很折腾人的问题,在ViewHolder模式是用以下方式给TextView控件赋值时报空指针异常:

public View getView(int position, View convertView, ViewGroup parent) {  
ViewHolder holder = null;  
if(holder == null)  
{  
holder = new ViewHolder();  
if(convertView == null)  
{  
convertView = mInflater.inflate(R.layout.inbox_listitem, null);  
System.out.println("convertView----------------->"+convertView);  
}  
holder.image = (ImageView)findViewById(R.id.connecter_image);  
holder.nametxt = (TextView)findViewById(R.id.connecter_name);  
holder.messagetxt = (TextView)findViewById(R.id.connect_message);  
System.out.println("holder.image----->"+holder.image);//此处出现空指针  
System.out.println("holder.nametxt ---->"+holder.nametxt);//此处出现空指针  
System.out.println("holder.messagetxt ---->"+holder.messagetxt);//此处出现空指针  
convertView.setTag(holder);  
}else  
{  
holder = (ViewHolder) convertView.getTag();  
}  
System.out.println("position------------------->" +position);  
System.out.println("mSmsPic.get(position)------------->" +mSmsPic.get(position));  
System.out.println("mContactsName.get(position)------------->" +mContactsName.get(position));  
System.out.println("mContactsmessage.get(position)------------->" +mContactsmessage.get(position));  
holder.image.setImageBitmap(mSmsPic.get(position));  <span style="font-family: simsun;">//此处出现空指针</span>
holder.nametxt.setText(mContactsName.get(position));  <span style="font-family: simsun;">//此处出现空指针</span>
holder.messagetxt.setText(mContactsmessage.get(position));   <span style="font-family: simsun;">//此处出现空指针</span>
return convertView;  
}  


怎么调试都无效,最后被逼无奈改用SimpleAdapter……可是,问题还是在心里纠结着!!BaseAdapter也不是第一次用了,参照以前的code居然也没发现问题,其实还是蛮明显的(都怪老眼昏花,欲哭无泪啊!),都是疏忽大意惹的祸!!

今天闲了,根据现象,百度了一下…… 结果,坑爹啊,原来原因就少添加了一个convertView。参见下面代码:

public View getView(int position, View convertView, ViewGroup parent) {  
ViewHolder holder = null;  
if(holder == null)  
{  
holder = new ViewHolder();  
if(convertView == null)  
{  
convertView = mInflater.inflate(R.layout.inbox_listitem, null);  
System.out.println("convertView----------------->"+convertView);  
}  
holder.image= (ImageView)<span style="color:#ff0000;">convertView</span>.findViewById(R.id.connecter_image);  
holder.nametxt =(TextView)<span style="color:#ff0000;">convertView</span>.findViewById(R.id.connecter_name);  
holder.messagetxt = (TextView) <span style="color:#ff0000;">convertView</span>.findViewById(R.id.connect_message);  
System.out.println("holder.image----->"+holder.image);  
System.out.println("holder.nametxt ---->"+holder.nametxt);  
System.out.println("holder.messagetxt ---->"+holder.messagetxt);  
convertView.setTag(holder);  
}else  
{  
holder = (ViewHolder) convertView.getTag();  
}  
System.out.println("position------------------->" +position);  
System.out.println("mSmsPic.get(position)------------->" +mSmsPic.get(position));  
System.out.println("mContactsName.get(position)------------->" +mContactsName.get(position));  
System.out.println("mContactsmessage.get(position)------------->" +mContactsmessage.get(position));  
holder.image.setImageBitmap(mSmsPic.get(position));  
holder.nametxt.setText(mContactsName.get(position));  
holder.messagetxt.setText(mContactsmessage.get(position));  
return convertView;  
}  

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:30159次
    • 积分:530
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:30篇
    • 译文:0篇
    • 评论:1条