我只想加载我手机的所有联系人,下面是源代码。我也关闭了光标,我不知道为什么会发生这种错误,有人可以告诉我导致此错误的原因以及解决方法。
Cursor cursor = null;
try {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
cursor = mActivity.getContentResolver().query(uri,new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY }, null, null, null);
while (cursor != null && cursor.moveToNext()) {
String number = cursor.getString(0);
String name = cursor.getString(1);
String id = cursor.getString(2);
String key = cursor.getString(3);
number = Utils.trimTelNum(number);
LogUtil.i(TAG, "contacts number=" + number + ",name=" + name);
if (number == null || name == null) {
Log.e(TAG, "contacts name or number is null,continue");
continue;
}
CalllogBean bean = new CalllogBean();
bean.setContactId(id);
bean.setNumber(number);
bean.setContactName(name);
bean.setLookupKey(key);
contactsMap.put(number, bean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// close the cursor
if (cursor != null) {
cursor.close();
cursor = null;
}
}