异常:读取联系人数据库异常处理

最近写了一个读取联系人数据库的模块,奇怪的是在模拟器上没有问题可是放到真机上测试,就会崩溃报了下述异常。

Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null

搜索相关文档,这个异常表示查询数据库的查询条件为空。可是明明定义的搜索条件,百思不得其解只能单步调试加打Log判断结果。结果发现了在真机上可能存在的问题就是查询的数据id,MIME类型,甚至是联系人id都可能为空。如果拿空的contact_id进行查找当然会报搜索条件为空的异常。之后我把代码插入了判断为空的部分,终于可以正常查找了。

Uri rawContactsUri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri data = Uri.parse("content://com.android.contacts/data");
        Cursor rawContactsCursor = getContentResolver().query(rawContactsUri, new String[]{"contact_id"}, null, null, null);
        Log.i("rawContactsCursor","" +rawContactsCursor.getCount());
        if(rawContactsCursor!=null)
        {
            while(rawContactsCursor.moveToNext())
            {
                if(rawContactsCursor.isNull(0))    //联系人ID都能为空,不用说了,不管这项罢了
                    continue;
                String contactid = rawContactsCursor.getString(rawContactsCursor.getColumnIndex("contact_id"));
                Cursor dataCursor = getContentResolver().query(data, new String[]{"data1","mimetype"}, "contact_id=?", new String[]{contactid}, null);
                if(dataCursor!=null)  //取出某一个联系人的所有信息
                {
                    Map<String,Object> map = new HashMap<String, Object>();
                    while(dataCursor.moveToNext())
                    {
                        String mimetype = null;
                        if(dataCursor.isNull(0))  //数据内容为空,则相应数据类型写为空
                        {
                            mimetype = dataCursor.getString(1);
                            if("vnd.android.cursor.item/phone_v2".equals(mimetype))
                            {
                                map.put("phone", "电话号码为空");
                            }
                            else if("vnd.android.cursor.item/name"
                                    .equals(mimetype))
                            {
                                map.put("name", "姓名为空");
                            }
                        }
                        if(dataCursor.isNull(1))        //数据MIME类型为空,则跳过
                        {
                            continue;
                        }
                        //数据正常,写入
                        String data1 = dataCursor.getString(0);
                        mimetype = dataCursor.getString(1);
                        if("vnd.android.cursor.item/phone_v2".equals(mimetype))
                        {
                            map.put("phone", data1);
                        }
                        else if("vnd.android.cursor.item/name"
                                .equals(mimetype))
                        {
                            map.put("name", data1);
                        }
                    }
                    contactors.add(map);
                    dataCursor.close();  //下一次获取联系人就不会重复了
                }
            }
            rawContactsCursor.close();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值