关于在an​​droid中获取Contact的问题

时间:2014-06-16 12:46:28

标签: android android-contacts

我开发了一个应用程序,它具有保存电话号码和从电话目录获取联系的功能

但我的代码中出现错误

我的代码是

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

    if (requestCode == PICK_CONTACT && resultCode == getActivity().RESULT_OK && null != data) {


        Uri contactData = data.getData();
        Cursor c = getActivity().getContentResolver().query(contactData, null, null, null, null);
        if (c.moveToFirst()) {
            String name = c.getString(c.getColumnIndexOrThrow(Contacts.People.NAME));
            String number = c.getString(c.getColumnIndexOrThrow(Contacts.People.NUMBER));
            friendMobile.setText(name);
            Toast.makeText(getActivity(), name + " has number " + number, Toast.LENGTH_LONG).show();


        }
    }
}

错误日志

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131074, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r2-2B352F4D2741/2 flg=0x1 }} to activity {com.app./com.app.MainActivity}: java.lang.IllegalArgumentException: column 'name' does not exist
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
        at android.app.ActivityThread.access$1100(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

Try this way,hope this will help you to solve your problem.

Note : add read contact permission in AndroidManifest.xml

String id="";
String no="";
String name="";

Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
while(cursor.moveToNext()){
      id = cursor.getString(cursor.getColumnIndex("_id"));
      if("1".equalsIgnoreCase(cursor.getString(cursor.getColumnIndex("has_phone_number")))){
      Cursor cursorNo = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "contact_id = " + id, null, null);
      while (cursorNo.moveToNext()) {
         if (cursorNo.getInt(cursorNo.getColumnIndex("data2")) == 2){
           no = no.concat(cursorNo.getString(cursorNo.getColumnIndex("data1")));
           break;
         }
      }
      cursorNo.close();
      }
}
cursor.close();

String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, id };
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
   name= nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
}
nameCur.close();
Toast.makeText(this," Name : "+name+" No : "+no,Toast.LENGTH_SHORT).show();