问题
I am getting an error when I want to access the a specific contact.
java.lang.IllegalArgumentException: Invalid column contact_id
Here is the sample code:
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();
}
The error is in the projection but I am not sure how to fix it. The number is saved on the phone under test. Any advice on solving the problem would be much appreciated.
回答1:
Just change ContactsContract.PhoneLookup.CONTACT_ID
to ContactsContract.PhoneLookup._ID
.
The _ID
in PhoneLookup
simply mean the CONTACT_ID
See here: https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html
回答2:
Use ContactsContract.CommonDataKinds.Phone.CONTENT_URI
instead of ContactsContract.PhoneLookup.CONTACT_ID
来源:https://stackoverflow.com/questions/45043662/error-in-looking-up-a-contact-android