开发者

IllegalArgument Accepting onActivityResult from Contacts?

开发者 https://www.devze.com 2023-04-13 01:43 出处:网络
I am using this method to return a result code and then get a contacts information from the contacts database.

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()开发者_JS百科;
                    }

                    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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消