2 u011732740 u011732740 于 2015.06.18 11:01 提问

Android中重写SimpleAdapter类引用后报空指针异常

直接粘代码吧 - -
重写的SimpleAdapter类:

 public class MySimpleAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> list;
    private int layoutID;
    private String flag[];
    private int ItemIDs[];
    public MySimpleAdapter(Context context, ArrayList<HashMap<String, Object>> list,
            int layoutID, String flag[], int ItemIDs[]) {
        this.mInflater = LayoutInflater.from(context);
        this.list = list;
        this.layoutID = layoutID;
        this.flag = flag;
        this.ItemIDs = ItemIDs;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(layoutID, null);
        convertView = mInflater.inflate(layoutID, null);
        for (int i = 0; i < flag.length; i++) {
            if (convertView.findViewById(ItemIDs[i]) instanceof ImageView) {
                ImageView iv = (ImageView) convertView.findViewById(ItemIDs[i]);
                iv.setBackgroundResource((Integer) list.get(position).get(
                        flag[i]));
            } else if (convertView.findViewById(ItemIDs[i]) instanceof TextView) {
                TextView tv = (TextView) convertView.findViewById(ItemIDs[i]);
                tv.setText((String) list.get(position).get(flag[i]));
            }
        }
        addListener(convertView);
        return convertView;
    }

    public void addListener(View convertView) {
        ((TextView)convertView.findViewById(R.id.order_mine_show_handle_confirm)).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new AlertDialog.Builder(OrderMineNonPaymentActivity.onp)
                        .setTitle("自定义通用SimpleAdapter")
                        .setMessage("按钮成功触发监听事件!")
                        .show();
                    }
                });

    }
}

引用的地方 OrderMineNonPaymentActivity:

 /**
     * 动态生成订单列表 lipeng
     */
    private void addNonPaymentList() {
        order_mine_show_non_payment_list_ll = (LinearLayout) findViewById(R.id.order_mine_show_non_payment_list_ll);
        nonPaymentList = (ListView) findViewById(R.id.order_mine_show_non_payment_list);
        nonPaymentList.setDivider(null);

        nonPaymentSimpleAdapter = new SimpleAdapter(
                OrderMineNonPaymentActivity.this, goodsList,
                R.layout.activity_order_non_payment_item, new String[] {
                        "shopname", "bigimage", "goodname" }, new int[] {
                        R.id.order_mine_show_shopname,
                        R.id.order_mine_show_goods_img,
                        R.id.order_mine_show_goods_name });
        MySimpleAdapter nonPaymentSimpleAdapter = new MySimpleAdapter(this,
                goodsList, R.layout.activity_order_mine_non_payment,
                new String[] { "shopname", "bigimage", "goodname" }, new int[] {
                        R.id.order_mine_show_shopname,
                        R.id.order_mine_show_goods_img,
                        R.id.order_mine_show_goods_name });
        nonPaymentList.setAdapter(nonPaymentSimpleAdapter);
    }

    private class MyOnItemClickListener implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }

    }

异常报的空指针,找了半天也没找到为啥空指针了

 06-17 22:55:32.845: E/AndroidRuntime(1463): FATAL EXCEPTION: main
06-17 22:55:32.845: E/AndroidRuntime(1463): Process: com.zb.stlala, PID: 1463
06-17 22:55:32.845: E/AndroidRuntime(1463): java.lang.NullPointerException
06-17 22:55:32.845: E/AndroidRuntime(1463):     at com.zb.stlala.adapter.MySimpleAdapter.addListener(MySimpleAdapter.java:83)
06-17 22:55:32.845: E/AndroidRuntime(1463):     at com.zb.stlala.adapter.MySimpleAdapter.getView(MySimpleAdapter.java:75)

4个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.06.18 11:15
已采纳
 ((TextView)convertView.findViewById(R.id.order_mine_show_handle_confirm))
 看看这个convertView里有id=order_mine_show_handle_confirm的textview吗
u011732740
u011732740 回复danielinbiti: 嗯嗯,谢谢,会注意的,直接刚开始转Android,公司就给项目了,就尝试去做了,也是在摸石头过河
2 年多之前 回复
danielinbiti
danielinbiti 回复SaluteWarmTinct: 呃,又是你呀,倒没看谁提的。建议你多看些基础吧。这上面代码结构还是差了些,用了多次没必要的findViewById,这消耗性能
2 年多之前 回复
u011732740
u011732740 搞定了,convertView这个对象是不是没有inflate出来,谢谢啦,我这一有问题你就来了,我都不知道咋感谢了
2 年多之前 回复
u011732740
u011732740 没有的,获取不到,不知道为什么拿不到
2 年多之前 回复
bdmh
bdmh   Ds   Rxr 2015.06.18 11:07

addListener里报空指针,是不是find没有找到对象,调试一下就清楚了

u011732740
u011732740 搞定了,convertView这个对象是不是没有inflate出来,谢谢
2 年多之前 回复
u011732740
u011732740 调试了,是find没找到对象,可是为什么呢
2 年多之前 回复
Arnold9009
Arnold9009   2015.06.18 11:33

convertView这个对象是不是没有inflate出来

u011732740
u011732740 谢谢啦,搞定了
2 年多之前 回复
u013263323
u013263323   2015.06.21 19:00

for (int i = 0; i < flag.length; i++) {
if (convertView.findViewById(ItemIDs[i]) instanceof ImageView) {
ImageView iv = (ImageView) convertView.findViewById(ItemIDs[i]);
iv.setBackgroundResource((Integer) list.get(position).get(
flag[i]));
} else if (convertView.findViewById(ItemIDs[i]) instanceof TextView) {
TextView tv = (TextView) convertView.findViewById(ItemIDs[i]);
tv.setText((String) list.get(position).get(flag[i]));
}
}

ListView在第一次实例化条目的时候,convertView为null,所以报了空指针异常,一般都是先判断convertView是否为空,如果不为空,进行服用,以节约内存,其次最好写个ViewHolder用来存储ListView的item中的各个控件的引用,因为每次调用findViewById,是非常耗费时间的。。。。
Csdn user default icon
上传中...
上传图片
插入图片