最近写了一个读取联系人数据库的模块,奇怪的是在模拟器上没有问题可是放到真机上测试,就会崩溃报了下述异常。
Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
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))
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))
{
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();
}