从设备联系人处获取电子邮件地址时获取FATAL异常

时间:2013-07-01 17:52:51

标签: android android-contentprovider android-contacts

我正在尝试使用以下代码从设备联系人处获取电子邮件地址,但我得到致命例外。 请帮忙解决我的问题。

07-01 23:15:34.984: E/AndroidRuntime(22688): FATAL EXCEPTION: main
07-01 23:15:34.984: E/AndroidRuntime(22688): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/2292i7f5b6c750a4e6301/4631 flg=0x1 (has extras) }} to activity {kc.RajusLabs.iamhere/kc.RajusLabs.iamhere.Email}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.os.Looper.loop(Looper.java:130)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at java.lang.reflect.Method.invoke(Method.java:507)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at dalvik.system.NativeStart.main(Native Method)
07-01 23:15:34.984: E/AndroidRuntime(22688): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.database.CursorWrapper.getString(CursorWrapper.java:135)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at kc.RajusLabs.iamhere.Email.onActivityResult(Email.java:115)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
07-01 23:15:34.984: E/AndroidRuntime(22688):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
07-01 23:15:34.984: E/AndroidRuntime(22688):    ... 11 more

CODE ::

   if (resultCode == Activity.RESULT_OK) {
          Uri contactData = data.getData();
          Cursor c = managedQuery(contactData, null, null, null, null);
          c.moveToFirst();
          String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
          String name1 = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
          String ContactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

       if(Integer.parseInt(name1) == 1){
         Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
                                                    ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + ContactID, null, null);
         String emailIdOfContact = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
         Toast.makeText(getApplicationContext(), "NAME:: "+name+"ContactID:: "+ContactID, Toast.LENGTH_LONG).show();
         textMessage.setText(name+ContactID);

          }
          }
            break;

我正在

获得例外
  

emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

2 个答案:

答案 0 :(得分:2)

更改行

c.moveToFirst(); 

if(!c.moveToFirst())
 return;

这里C是光标,与其他光标一样,也像“电子邮件”一样

您正在尝试从空光标中获取值。

答案 1 :(得分:1)

您没有在moveToFirst()光标

上调用emails

添加

emails.moveToFirst();

 Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null, ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + ContactID, null, null);

你应该总是确认你已成功移动到AAnkit在他的回答中建议的第一个位置。我已更新您的代码以包含这些检查以及修复原始问题并验证您的DATA列不为空

if (resultCode == Activity.RESULT_OK) {
    Uri contactData = data.getData();
    Cursor c = managedQuery(contactData, null, null, null, null);
    if (!c.moveToFirst())
        return;
    String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
    String name1 = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    String ContactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

    if (Integer.parseInt(name1) == 1) {
        Cursor emails = getContentResolver().query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + ContactID, null, null);
        if (!emails.moveToFirst())
            return;
        int index = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)
        if (!emails.isNull(index)) {
            String emailIdOfContact = emails.getString(index);
            Toast.makeText(getApplicationContext(), "NAME:: " + name + "ContactID:: " + ContactID,
                    Toast.LENGTH_LONG).show();
            textMessage.setText(name + ContactID);
        }

    }
}
break;