android.database.CursorIndexOutOfBoundsException : Index -1 requested, 大小为 2

标签 android contacts android-cursor

下面是我的代码,我得到了 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/

相关文章:

java - 通过android创建sqlite数据库

android - 获取所有 Android 设备图像

java - Android,使用 "provided project "时出现 ClassNotFoundException 或使用 "compile project"时出现 IllegalArgumentException : already added ,

android - Realm Java 作为对原生 Android 库的依赖绑定(bind)到 Xamarin

android - Actionbarsherlock getHeight() 返回 0

java - 打开联系人后如何直接调用电话

Android - 屏幕中的图像散射

ruby - 使用 omnicontacts gem 在 ruby​​ 脚本中获取雅虎联系人

iOS 应用程序在后台运行并在插入联系人时收到通知

java - 如何在系统重新启动后停止正在运行的下载