0

我正在使用意图检索电话号码。它正在显示联系人列表。现在我想找一个联系人。但是当我点击任何一个联系人时,出现异常。它发生在号码检索期间,但对于联系人姓名检索是正确的。

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, 2); 


Uri contactUri = data.getData(); 
        //String[] projection = {Phone.NUMBER};

              Cursor cursor = getContentResolver()
                .query(contactUri, null, null, null, null);
              cursor.moveToNext();

             // String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// name got me.
              String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // exception for phone number.

              System.out.println("name "+name+phone);

日志猫:-

 06-21 18:06:38.394: E/AndroidRuntime(30700): java.lang.RuntimeException: Failure delivering      result ResultInfo{who=null, request=2, result=-1, data=Intent {    dat=content://com.android.contacts/contacts/lookup/1135r4-2727494D372D37.3754r14-2727494D372D37/4 }}    to activity {com.MednymaSecureChat/com.MednymaSecureChat.XMPPClient}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.access$1100(ActivityThread.java:123)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.os.Looper.loop(Looper.java:137)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at java.lang.reflect.Method.invokeNative(Native Method)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at java.lang.reflect.Method.invoke(Method.java:511)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at dalvik.system.NativeStart.main(Native Method)
06-21 18:06:38.394: E/AndroidRuntime(30700): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.database.CursorWindow.nativeGetString(Native Method)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.database.CursorWindow.getString(CursorWindow.java:442)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at com.MednymaSecureChat.XMPPClient.onActivityResult(XMPPClient.java:480)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.Activity.dispatchActivityResult(Activity.java:4654)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
06-21 18:06:38.394: E/AndroidRuntime(30700):    ... 11 more

请任何人帮助我

4

2 回答 2

2
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
startActivityForResult(intent, 1);  

// ...

@Override  
public void onActivityResult(int reqCode, int resultCode, Intent data) {  
    super.onActivityResult(reqCode, resultCode, data);  
    if (resultCode == Activity.RESULT_OK) {  
        Uri contactData = data.getData();  
        Cursor c =  managedQuery(contactData, null, null, null, null);  
        if (c.moveToFirst()) {  
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));  
           Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
  ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
    phones.moveToFirst();
    String phone= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      tv.setText(name+" "+phone);
        }  
    }  
}
于 2013-06-21T12:47:57.480 回答
2

试试这个代码

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {
        if (requestCode == 2)
        {
         Uri contactUri = data.getData(); 
        //String[] projection = {Phone.NUMBER};

              Cursor cursor = getContentResolver()
                .query(contactUri, null, null, null, null);
              cursor.moveToNext();

             // String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// name got me.
              String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // exception for phone number.

              System.out.println("name "+name+phone);
    }
  }

(或)尝试以下链接堆栈帖子以选择联系人编号并命名它对我有用

从 android 应用程序的联系人列表中选择一个号码和姓名

于 2013-06-21T12:44:06.077 回答