开发者

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2

开发者 https://www.devze.com 2023-02-07 20:19 出处:网络
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?

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.TYP开发者_开发问答E));
    i++;
   } 
  }
 }
}


If you are accessing data from Cursor object than you must have to position the Cursor 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 line Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); in your code.

And also ensure that you are not using and older API for retrieving Contacts.

0

精彩评论

暂无评论...
验证码 换一张
取 消