IllegalArgument 接受来自联系人的 onActivityResult?

发布于 2024-12-09 21:05:33 字数 2715 浏览 3 评论 0原文

我使用此方法返回结果代码,然后从联系人数据库中获取联系人信息。

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

     switch (requestCode) {
        case (CONTACT_PICKER_RESULT) :
          if (resultCode == Activity.RESULT_OK) {
              Uri contactData = data.getData();
              Cursor cursor =  managedQuery(contactData, null, null, null, null);
              while (cursor.moveToNext()) 
                {           
                    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                   name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

                    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

if ( hasPhone.equalsIgnoreCase("1"))
                        hasPhone = "true";
                    else
                        hasPhone = "false" ;

if (Boolean.parseBoolean(hasPhone)) 
                    {
                     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();
                    }

                    break;

            }
            ContactInfo.setText("Contact: " + name + "./n" + " Phone Number: "+ phoneNumber);
             }
          }

     }

问题是我不断收到此错误。

10-13 17:27:19.017: ERROR/AndroidRuntime(6781): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/data/229 flg=0x1 (has extras) }} to activity {com.fttech.test2/com.fttech.test2.TestContacts}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed

10-13 17:27:19.017: ERROR/AndroidRuntime(6781): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
10-13 17:27:19.017: ERROR/AndroidRuntime(6781):     at android.database.CursorWindow.getString_native(Native Method)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781):     at android.database.CursorWindow.getString(CursorWindow.java:375)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781):     at android.database.CursorWrapper.getString(CursorWrapper.java:135)

I am using this method to return a result code and then get a contacts information from the contacts database.

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

     switch (requestCode) {
        case (CONTACT_PICKER_RESULT) :
          if (resultCode == Activity.RESULT_OK) {
              Uri contactData = data.getData();
              Cursor cursor =  managedQuery(contactData, null, null, null, null);
              while (cursor.moveToNext()) 
                {           
                    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                   name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

                    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

if ( hasPhone.equalsIgnoreCase("1"))
                        hasPhone = "true";
                    else
                        hasPhone = "false" ;

if (Boolean.parseBoolean(hasPhone)) 
                    {
                     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();
                    }

                    break;

            }
            ContactInfo.setText("Contact: " + name + "./n" + " Phone Number: "+ phoneNumber);
             }
          }

     }

The problem is i keep getting this error.

10-13 17:27:19.017: ERROR/AndroidRuntime(6781): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/data/229 flg=0x1 (has extras) }} to activity {com.fttech.test2/com.fttech.test2.TestContacts}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed

10-13 17:27:19.017: ERROR/AndroidRuntime(6781): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
10-13 17:27:19.017: ERROR/AndroidRuntime(6781):     at android.database.CursorWindow.getString_native(Native Method)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781):     at android.database.CursorWindow.getString(CursorWindow.java:375)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781):     at android.database.CursorWrapper.getString(CursorWrapper.java:135)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2024-12-16 21:05:33

我认为您的问题出在其中的一些 cursor.getColumnIndex 上,也许 ContactsContract.Contacts._ID 就是问题所在。
但我给您的建议是逐一调试,并监视您在该代码中拥有的所有 ContactsContract.* 访问

返回 -1 的人就是给你带来问题的人。

I think your problem is located in some of that cursor.getColumnIndex , maybe ContactsContract.Contacts._ID is the problem ones .
But my tip for you is debug one by one putting a watch on all ContactsContract.* access you have in that code .

The ones who returns a -1 is the one who is giving you the problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文