android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为2
下面是我的代码,我得到了 android.database.CursorIndexOutOfBoundsException
: Index -1 requests, 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++;
}
}
}
}
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++;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要从
Cursor
对象访问数据,则必须定位Cursor
对象。实际上,在尝试从中访问数据之前,您必须将光标定位到第一行。
将行
cur.moveToFirst();
放在行Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null); 在您的代码中。
还要确保您没有使用旧版 API 来检索联系人。
If you are accessing data from
Cursor
object than you must have to position theCursor
object.Actually you have to position
Cursor
to the first row before you try to access data from it.Put the line
cur.moveToFirst();
after the lineCursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
in your code.null, null, null, null);
And also ensure that you are not using and older API for retrieving Contacts.