I have been attempting to Read Contacts and just for the moment Log them to the console but I am getting a RunTime Exception error on my app. I have been following this tutorial https://www.youtube.com/watch?v=g4_1UOFNLEY&t=1s
I am not sure if this is the correct way of getting contact details and would appreciate any advice. My end goal would be to eventually have a contact picker where a user can choose a contact and add their name to a list
I have the permissions working correctly (as cleared up by another user in another thread) but still having problems.
Method called in onCreate:
private void viewContacts(){
// TODO CONTACT INFO - will log to console
ContentResolver resolver = getContentResolver();
// to gets all contacs
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()){
// Access specific field of contact
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//get phone numbers
Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{ contactId }, null);
Log.i("MY INFO", contactId + " = " +contactName);
//get phone num
while(phoneCursor.moveToNext()){
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("MY INFO", phoneNumber);
}
// get email details
Cursor emailCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",new String[]{ contactId}, null);
while(emailCursor.moveToNext()){
String contactEmail = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.i("MY INFO", contactEmail);
}
}
Error
/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{AppName.EditPlayersActivity}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.database.CursorWrapper.getString(CursorWrapper.java:142)
at AppName.EditPlayersActivity.viewContacts(EditPlayersActivity.java:175)
at AppName.EditPlayersActivity.permissionsCheck(EditPlayersActivity.java:222)
at AppName.EditPlayersActivity.onCreate(EditPlayersActivity.java:132)
at android.app.Activity.performCreate(Activity.java:6248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
04-24 17:37:48.206 19943-19943/AppName.squad E/AndroidRuntime: FATAL EXCEPTION: main
Process: AppName.squad, PID: 19943
java.lang.RuntimeException: Unable to start activity ComponentInfo{AppName.squad/AppName.squad.EditPlayersActivity}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.database.CursorWrapper.getString(CursorWrapper.java:142)
at AppName.squad.EditPlayersActivity.viewContacts(EditPlayersActivity.java:175)
at AppName.squad.EditPlayersActivity.permissionsCheck(EditPlayersActivity.java:222)
at AppName.squad.EditPlayersActivity.onCreate(EditPlayersActivity.java:132)
at android.app.Activity.performCreate(Activity.java:6248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
答案 0 :(得分:0)
您提供的代码:
Cursor emailCursor =
resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{contactId},
null);
您似乎在查询手机内容提供商中的电子邮件。您不应该使用ContactsContract.CommonDataKinds.Email.CONTENT_URI
吗?