android.database.CursorIndexOutOfBoundsException:索引-1请求,大小为2 [英] android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
问题描述
下面是我的代码,我得到 android.database.CursorIndexOutOfBoundsException
:索引-1请求,大小为2的错误。任何人都可以告诉我如何解决它?
Below is my codes and I got the android.database.CursorIndexOutOfBoundsException
: Index -1 requested, with a size of 2 error. Can anyone tell me how to solve it?
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
object。
If you are accessing data from Cursor
object than you must have to position the Cursor
object.
实际上,在尝试访问数据之前,您必须将 Cursor
位于第一行。
Actually you have to position Cursor
to the first row before you try to access data from it.
在后面放置
cur.moveToFirst();
。
此外,您还可以在自己的代码中添加.query(ContactsContract.Contacts.CONTENT_URI,
null,null,null,null); 请确保您没有使用旧的API来检索联系人。
And also ensure that you are not using and older API for retrieving Contacts.
这篇关于android.database.CursorIndexOutOfBoundsException:索引-1请求,大小为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!