2012-10-05 25 views
7

Tôi cố gắng để chọn địa chỉ liên lạc với số điện thoại only.And tôi sau this codeAndroid: Liên hệ Picker Intent | không thể nhanh chóng loại 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); 
} 

Nhưng thật không may, cho thấy nó có lỗi: Mã Cannot instantiate the type Uri

Thật sự tôi đã làm việc khác mà đang hoạt động hoàn hảo nhưng gặp sự cố khi chọn Danh bạ email. Tôi chỉ cần số điện thoại.

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

và tại onReceive(), phương pháp này được gọi là

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(); 
} 

Trả lời

10

này làm việc cho tôi:

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:

onActivityResult() của bạn sẽ trông như thế này:

@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

gì dòng mã là gây ra ngoại lệ này? bạn có thể dán toàn bộ ngăn xếp trong câu hỏi không? bạn có định nghĩa onActivityResult() của mình không? –

+0

Đã sửa lỗi. :) Đó là một sửa chữa đơn giản .. thực sự sai lầm của tôi .. cảm ơn thời gian quý báu của bạn :) –

5

Sử dụng Uri.parse() để thay thế. Bạn không thể instsntiate một Uri trực tiếp

Các vấn đề liên quan