每当我尝试访问联系人的号码时,我都会得到索引超出范围的异常,有人可以告诉我这里我做错了什么。
班级成员
private String[] names;
private String[] number;
在我的onCreate
中ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0)
{
names = new String[cur.getCount()];
number = new String[cur.getCount()];
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
names[cur.getPosition()] = name;
// number[cur.getPosition()] = name;
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
try{
number[cur.getPosition()] = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
catch(Exception e)
{
Log.d("Exception", e.toString());
}
phones.close();
}
}
}
堆栈跟踪
/AndroidRuntime(10284): FATAL EXCEPTION: main
E/AndroidRuntime(10284): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.informationkinetics.SMSS/com.informationkinetics.SMSS.Contact}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
E/AndroidRuntime(10284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
E/AndroidRuntime(10284): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2621)
E/AndroidRuntime(10284): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
E/AndroidRuntime(10284): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
E/AndroidRuntime(10284): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
E/AndroidRuntime(10284): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
E/AndroidRuntime(10284): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
E/AndroidRuntime(10284): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
E/AndroidRuntime(10284): at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(10284): at android.view.View$PerformClick.run(View.java:8817)
E/AndroidRuntime(10284): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(10284): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(10284): at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime(10284): at android.app.ActivityThread.main(ActivityThread.java:4914)
E/AndroidRuntime(10284): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10284): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(10284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
E/AndroidRuntime(10284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/AndroidRuntime(10284): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(10284): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
E/AndroidRuntime(10284): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
E/AndroidRuntime(10284): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
E/AndroidRuntime(10284): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
E/AndroidRuntime(10284): at android.database.CursorWrapper.getString(CursorWrapper.java:140)
E/AndroidRuntime(10284): at com.informationkinetics.SMSS.Contact.onCreate(Contact.java:44)
E/AndroidRuntime(10284): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
E/AndroidRuntime(10284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
E/AndroidRuntime(10284): ... 18 more
答案 0 :(得分:1)
尝试添加
cur.moveToFirst();
在您的while循环之前,并将while(){}
更改为do{} while()
编辑:对phones
光标执行相同操作:
添加
phone.moveToFirst();
在尝试{}声明之前。