关于ListView控件下拉崩溃的注意事项

来源:互联网 发布:淘宝一般用什么快递 编辑:程序博客网 时间:2024/06/01 21:27

        最近在做一个小作品,其中用到了ListView控件显示图片等内容,但是在使用的过程中遇到了下拉的时候会是程序崩溃,网上找了很多,也没有专门的帖子说明这个问题。

后来在看到别人讲解ListView使用的例子博客的时候(原帖链接没找到了,实在很抱歉),评论区中发现有网友说博主不能使用“if(convertView==null)”这句话,当时并没有太在意,后来自己又重新写了一个listView测试的例子,没有加上这句话,就不会出现程序崩溃的问题。

        下面贴出出问题的代码片段

@Override    public View getView(int position, View convertView, ViewGroup parent) {        component = new Component();        //获得组件,实例化组件        if(convertView == null) {            convertView = layoutInflater.inflate(R.layout.listview, null);            component.image = (ImageView) convertView.findViewById(R.id.image);            component.description = (TextView) convertView.findViewById(R.id.imagename);            component.price = (TextView) convertView.findViewById(R.id.price);            component.purchase = (TextView) convertView.findViewById(R.id.purchase);        }        //绑定数据        System.out.println((String)data.get(position).get("image")); //数据输出测试        component.image.setTag((String)data.get(position).get("image"));        loader.loadImage(2, (String)data.get(position).get("image"), component.image);        component.description.setText((String)data.get(position).get("imagename"));        component.price.setText("¥" + data.get(position).get("price"));        component.purchase.setText((String)data.get(position).get("purchase"));        return convertView;    }
       然后贴出程系统打印的异常

       05-31 18:04:58.745  24499-24499/com.shopretrieval E/InputEventReceiver﹕ Exception dispatching input event.

        当我把程序中出现的if(converView==null)删除之后,程序完美运行。

                                                                                                      

0 0
原创粉丝点击