2012-10-05 2 views
7

instanziiert Ich versuche, Kontakte mit Telefonnummer wählen only.And Ich verfolge this codeAndroid: Kontakt Picker Absicht | Kann nicht den Typ Uri

static final int PICK_CONTACT_REQUEST = 1; // The request code 
... 
private void pickContact() { 
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, new Uri("content://contacts")); 
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers 
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); 
} 

Aber leider seine zeigt einen Fehler: Cannot instantiate the type Uri

eigentlich die ich habe eine andere Arbeits Code funktioniert einwandfrei, stürzt aber bei der Auswahl von E-Mail-Kontakten ab. Ich brauche nur Telefonnummern.

Intent intentContact = new Intent(Intent.ACTION_PICK, 
           ContactsContract.Contacts.CONTENT_URI); 
intentContact.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivityForResult(intentContact, PICK_CONTACT); 

und onReceive() wird diese Methode

public void getContactInfo(Intent intent) { 

    ContentResolver cr = getContentResolver(); 
    cursor = cr.query(intent.getData(), null, null, null, null); 

    while (cursor.moveToNext()) { 
     String contactId = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Contacts._ID)); 
     if (Integer 
       .parseInt(cursor.getString(cursor 
         .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
      Cursor phones = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
          + " = " + contactId, null, null); 
      while (phones.moveToNext()) { 
       phoneNumber = phones 
         .getString(phones 
           .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      } 
      phones.close(); 
     } else { 
      snipp.showAlertDialog(getApplicationContext(), "No Number", 
        "Cannot read number", false); 
     } 

    } 
    cursor.close(); 
} 

Antwort

10

Dies funktioniert für mich instsntiate:

private void pickContact() { 
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); 
} 

Edit:

Ihre onActivityResult() sollte wie folgt aussehen:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent intent) { 

    super.onActivityResult(requestCode, resultCode, intent); 
    if (requestCode == PICK_CONTACT_REQUEST) { 

     if (resultCode == RESULT_OK) { 
       Uri pickedPhoneNumber = intent.getData(); 
       // handle the picked phone number in here. 
      } 
     } 
    } 
} 
+0

welche Codezeile ist verursacht diese Ausnahme? Kannst du den ganzen Stapel in die Frage einfügen? definierst du dein onActivityResult()? –

+0

Behoben. :) Es war eine einfache Lösung .. eigentlich mein Fehler ... Danke für Ihre wertvolle Zeit :) –

5

Verwendung Uri.parse() anstelle genannt. Sie können keine Uri direkt