下面是我的代码,我得到了 android.database.CursorIndexOutOfBoundsException
: Index -1 requested, with a size of 2 错误。谁能告诉我怎么解决?
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0) {
Cursor pCur = cr.query(
Contacts.Phones.CONTENT_URI,
null,
Contacts.Phones.PERSON_ID +" = ?",
new String[]{id}, null);
int i=0;
int pCount = pCur.getCount();
String[] phoneNum = new String[pCount];
String[] phoneType = new String[pCount];
while (pCur.moveToNext()) {
phoneNum[i] = pCur.getString(
pCur.getColumnIndex(Contacts.Phones.NUMBER));
phoneType[i] = pCur.getString(
pCur.getColumnIndex(Contacts.Phones.TYPE));
i++;
}
}
}
}
最佳答案
如果您从 Cursor
对象访问数据,则必须定位 Cursor
对象。
实际上,您必须先将 Cursor
定位到第一行,然后才能尝试从中访问数据。
将 cur.moveToFirst();
行放在 Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
在你的代码中。
并确保您没有使用旧 API 来检索联系人。
关于android.database.CursorIndexOutOfBoundsException : Index -1 requested, 大小为 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4847875/