我一直收到这个CursorOutOfBounds错误
10-13 22:30:10.205: ERROR/AndroidRuntime(16659): 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}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
10-13 22:30:10.205: ERROR/AndroidRuntime(16659): at android.app.ActivityThread.deliverResults(ActivityThread.java:2655)
10-13 22:30:10.205: ERROR/AndroidRuntime(16659): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
10-13 22:30:10.205: ERROR/AndroidRuntime(16659): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
-13 22:30:10.205: ERROR/AndroidRuntime(16659): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
我在onActivityREsult上使用此代码返回所选的联系人。
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));
Log.v("Contact_ID", contactId);
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Log.v("NAME", name);
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
phoneNumber = phones.getString(phones.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
ContactInfo.setText("Contact: " + name + "./n" + " Phone Number: "+ phoneNumber);
}
}