I'm trying to get a contact's number that the user selects. The problem is, whenever a contact is selected from the list, it throws this exception :
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
This occurs with any contact, even those with multiple phone numbers.
The only examples that opened the contact list for me :
Pick a Number and Name From Contacts List in android app
How to read contacts on Android 2.0
Android contacts database giving me an exception
They all produce the same error for me.
My code :
Calling the intent
public void pickPerson(View V)
{
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
Getting the phone number
//Declcared earlier
static final int PICK_CONTACT=1;
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode)
{
case (PICK_CONTACT) :
{
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst())
{
String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1"))
{
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null, null);
phones.moveToFirst();
String cNumber = phones.getString(phones.getColumnIndex(PhoneLookup.NUMBER));
phone.setText(cNumber); //EditText view
}
}
}
break;
}
}
}
Logcat :
11-29 12:35:17.770: E/AndroidRuntime(26254): FATAL EXCEPTION: main
11-29 12:35:17.770: E/AndroidRuntime(26254): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/data/1051 flg=0x1 }} to activity {somebadger.textspammer/somebadger.textspammer.Main}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.app.ActivityThread.deliverResults(ActivityThread.java:3208)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3251)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.app.ActivityThread.access$1200(ActivityThread.java:143)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.os.Looper.loop(Looper.java:137)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.app.ActivityThread.main(ActivityThread.java:4953)
11-29 12:35:17.770: E/AndroidRuntime(26254): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 12:35:17.770: E/AndroidRuntime(26254): at java.lang.reflect.Method.invoke(Method.java:511)
11-29 12:35:17.770: E/AndroidRuntime(26254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-29 12:35:17.770: E/AndroidRuntime(26254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-29 12:35:17.770: E/AndroidRuntime(26254): at dalvik.system.NativeStart.main(Native Method)
11-29 12:35:17.770: E/AndroidRuntime(26254): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
11-29 12:35:17.770: E/AndroidRuntime(26254): at somebadger.textspammer.Main.onActivityResult(Main.java:76)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.app.Activity.dispatchActivityResult(Activity.java:5344)
11-29 12:35:17.770: E/AndroidRuntime(26254): at android.app.ActivityThread.deliverResults(ActivityThread.java:3204)