查找联系人时出错 Android
Error in looking up a contact Android
当我想访问特定联系人时出现错误。
java.lang.IllegalArgumentException: Invalid column contact_id
示例代码如下:
String number = "0877777777";
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] projection = new String[]{ ContactsContract.PhoneLookup.CONTACT_ID };
Cursor cur = getActivity().getContentResolver().query(uri, projection, null, null, null);
// if other contacts have that phone as well, we simply take the first contact found.
if (cur != null && cur.moveToNext()) {
Long id = cur.getLong(0);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id));
intent.setData(contactUri);
startActivity(intent);
cur.close();
}
错误在投影中,但我不确定如何修复它。
号码保存在被测phone上。
任何解决问题的建议将不胜感激。
使用ContactsContract.CommonDataKinds.Phone.CONTENT_URI
代替ContactsContract.PhoneLookup.CONTACT_ID
只需将 ContactsContract.PhoneLookup.CONTACT_ID
更改为 ContactsContract.PhoneLookup._ID
。
PhoneLookup
中的 _ID
只是指 CONTACT_ID
看这里:https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html
当我想访问特定联系人时出现错误。
java.lang.IllegalArgumentException: Invalid column contact_id
示例代码如下:
String number = "0877777777";
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] projection = new String[]{ ContactsContract.PhoneLookup.CONTACT_ID };
Cursor cur = getActivity().getContentResolver().query(uri, projection, null, null, null);
// if other contacts have that phone as well, we simply take the first contact found.
if (cur != null && cur.moveToNext()) {
Long id = cur.getLong(0);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id));
intent.setData(contactUri);
startActivity(intent);
cur.close();
}
错误在投影中,但我不确定如何修复它。 号码保存在被测phone上。 任何解决问题的建议将不胜感激。
使用ContactsContract.CommonDataKinds.Phone.CONTENT_URI
代替ContactsContract.PhoneLookup.CONTACT_ID
只需将 ContactsContract.PhoneLookup.CONTACT_ID
更改为 ContactsContract.PhoneLookup._ID
。
PhoneLookup
中的 _ID
只是指 CONTACT_ID
看这里:https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html