奇怪的代码

来源:互联网 发布:响应式网站 知乎 编辑:程序博客网 时间:2024/05/17 02:59
  TextView title = null;
   TextView text = null;
//    if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null);

title =(TextView) convertView.findViewById(R.id.title);
text= (TextView) convertView.findViewById(R.id.content);
//    }
   
   title.setText("title");

   text.setText("content");


这代码写的是有问题的,不是我写的。因为!convertview==null后text,title就空指针了

不过要说的问题不是这个,用这代码列出五个列表的时候,五个列表都没有超出屏幕,根据网络资料来说convertView是绝对== null,也就是

title =(TextView) convertView.findViewById(R.id.title);
text= (TextView) convertView.findViewById(R.id.content);

是必然会运行的,也就是说title 是不会空指针的,但是,实际运行却错误,之后,我其他地方什么都没改,将convertview判空操作就给注释掉了,也就是上面的代码,之后就没有错误了,

两者唯一的区别就是   title.与text能不能得到实例,也就是  convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null);

title =(TextView) convertView.findViewById(R.id.title);
text= (TextView) convertView.findViewById(R.id.content);有没有执行,也就是convertView == null是不是真的成立!也就是说,即使没有超出屏幕,也可能不为null?

0 0
原创粉丝点击