2014-09-21 117 views
0

我想从我的联系人列表中我的应用程序中的联系人:选择从联系人列表中的联系人崩溃的应用程序

public void selecionar_contato(View view) { 
     Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 
     startActivityForResult(intent, CONTACT_PICKER_RESULT); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (resultCode == RESULT_OK) { 
      switch (requestCode) { 
      case CONTACT_PICKER_RESULT: 
       Uri dados = data.getData(); 
       Cursor c = getContentResolver().query(dados, new String[]{ 
         ContactsContract.CommonDataKinds.Phone.NUMBER, 
         ContactsContract.CommonDataKinds.Phone.TYPE }, null, null, null); 
       if(c.moveToFirst()){ 
        String num = c.getString(0); 
        int type = c.getInt(1); 
        mostarToast(type,num); 
       } 
       break; 
      } 

     } else { 
      // gracefully handle failure 
      Log.w("Erro", "Warning: ac"); 
     } 
    } 

    private void mostarToast(int type, String num) { 
     Toast.makeText(this, type + ": " + num, Toast.LENGTH_LONG).show(); 

    } 

但是,当我选择联系人,我的应用程序崩溃:

09-21 17:44:40.897: E/AndroidRuntime(17432): FATAL EXCEPTION: main 
09-21 17:44:40.897: E/AndroidRuntime(17432): Process: com.example.pacixmobile, PID: 17432 
09-21 17:44:40.897: E/AndroidRuntime(17432): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/298i107/602 flg=0x1 }} to activity {com.example.pacixmobile/com.example.pacixmobile.CadastroActivity}: java.lang.IllegalArgumentException: Invalid column data1 
09-21 17:44:40.897: E/AndroidRuntime(17432): at android.app.ActivityThread.deliverResults(ActivityThread.java:3551) 

我必须覆盖onActivityResult方法吗?我错过了什么?

回答

0

您请求的列不能直接用于您正在使用的Uri。您选择了一个联系人。你还没有选择一个电话号码。联系人可能有零个,一个或多个电话号码。

鉴于Uri摘自ContactsContract.Contacts,您可以检索列available on ContactsContract.Contacts

0

我有这个在一个活动,它的工作正常,在手机和平​​板电脑测试。 我首先得到选择的内容,然后是她/他的电话号码。我需要一个手机号码,如果它存在的话,它也是9位数(西班牙号码),去掉+34或任何国家代码。

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
    // super.onActivityResult(requestCode, resultCode, intent); 

    if (requestCode != 0x10 || resultCode != RESULT_OK) 
    { 
     super.onActivityResult(requestCode, resultCode, intent); 
     return; 
    } 
    Cursor cursor = null; 
    Uri contactUri = intent.getData(); 
    long contactId = -1; 
    // get display name from the contact 
    try 
    { 
     cursor = getContentResolver().query(contactUri, 
        new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }, null, null, 
        null); 
     if (cursor.moveToFirst()) 
     { 
      String name = cursor.getString(1); 
      contactId = cursor.getLong(0); 
      etNombre.setText(name); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (cursor != null) 
     { 
      cursor.close(); 
      cursor = null; 
     } 
    } 
    // do we have a valid contact ID? 
    if (contactId == -1) return; 

    // get all phone numbers with type from the contact 
    try 
    { 
     String tmpPhone = ""; 
     boolean itsDone = false, gotPhone = false; 
     cursor = getContentResolver().query(Phone.CONTENT_URI, new String[] { Phone.TYPE, Phone.NUMBER }, 
        Phone.CONTACT_ID + "=" + contactId, null, null); 
     // Pick up the first phone number 
     if (cursor.moveToFirst()) 
     { 
      tmpPhone = cursor.getString(1); 
      itsDone = cursor.getInt(0) == Phone.TYPE_MOBILE; 
      gotPhone = true; 
      // if Not a mobile, search others numbers 
      if (!itsDone) 
      { 
       while (cursor.moveToNext()) 
       { 
        if (cursor.getInt(0) == Phone.TYPE_MOBILE) 
        { 
         itsDone = true; 
         tmpPhone = cursor.getString(1); 
         break; 
        } 
       } 
      } 
     } 
     if (gotPhone) 
     { 
      int len = tmpPhone.length(); 
      if (len > 9) 
      { 
       tmpPhone = parsePhone(tmpPhone); 
       len = tmpPhone.length(); 
       if (len > 9) 
        tmpPhone = tmpPhone.substring(len - 9, len); 
      } 
      etTelefono.setText(tmpPhone); 
      etJornada.requestFocus(); 
     } 
     else etTelefono.requestFocus(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (cursor != null) 
     { 
      cursor.close(); 
     } 
    } 
} 

void cogerContactos() 
{ 
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    startActivityForResult(intent, 0x10); 
} 
相关问题