Error in looking up a contact Android

。_饼干妹妹 提交于 2019-12-25 08:59:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!