2017-07-11 1 views
0

Je reçois une erreur lorsque je souhaite accéder à un contact spécifique.Erreur lors de la recherche d'un contact Android

java.lang.IllegalArgumentException: colonne non valide contact_id

Voici l'exemple de 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(); 
       } 

L'erreur est dans la projection, mais je ne sais pas comment y remédier. Le numéro est enregistré sur le téléphone testé. Un conseil pour résoudre le problème serait très apprécié.

+0

De la [docs] (https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html) 'ContactsContract.PhoneLookup' contient seulement 3 colonnes. Aucun Contact_id là. Pour utiliser cette colonne, vous devez vous joindre à des contacts –

Répondre

0

Utilisation ContactsContract.CommonDataKinds.Phone.CONTENT_URI au lieu de ContactsContract.PhoneLookup.CONTACT_ID

+0

Cela me donnera un android.net.Uri. Comment pourrais-je implémenter cela dans le code ci-dessus? –