SaluteWarmTinct 2015-06-17 19:01 采纳率: 100%
浏览 2523
已采纳

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 2015-06-17 19:15
    关注
     ((TextView)convertView.findViewById(R.id.order_mine_show_handle_confirm))
     看看这个convertView里有id=order_mine_show_handle_confirm的textview吗
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)
  • ¥15 交叉注意力机制的残差问题
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?
  • ¥20 小规模孤立词识别系统设计
  • ¥15 关于Java对接海康威视车牌识别一体机SDK是否需要固定外网的IP?
  • ¥15 Linux扩容时,格式化卡住了:vgdispaly查看卷组信息,没有输出
  • ¥18 关于#ubuntu#的问题:使用背景-工作职责内有七八台ubuntu系统的电脑,平时需要互相调取资料,想实现把这几台电脑用交换机组成一个局域网,来实现指定文件夹的互相调取和写入
  • ¥20 求一个简易射频信号综测仪
  • ¥15 esp8266 tally灯 接收端改为发射端
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部