android.database.cursor 索引越界异常?

标签 android indexoutofboundsexception

我正在尝试检索手机中存储的联系人的姓名和类型,但出现此异常。我之前得到了名字但得到了异常

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

当试图一起检索名称和类型时。请帮忙。提前致谢。

package application.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;

import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.util.Log;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME};     
        String[] projection1=new String[]{Phone.TYPE};
        String[] projection2=new String[]{ContactsContract.Contacts._ID};

        ContentResolver cr = getContentResolver();
        ContentResolver ncr=getContentResolver();
        ContentResolver icr=getContentResolver();

        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
        Cursor ncur=ncr.query(ContactsContract.Data.CONTENT_URI, projection1, null, null,null);
        Cursor icur = icr.query(ContactsContract.RawContacts.CONTENT_URI, projection2,null, null, Contacts._ID + " ASC");

        if (cur.getCount() >0 && ncur.getCount()>0 && icur.getCount()>0) 
        {
            while (cur.moveToNext()&& ncur.moveToNext()&& icur.getCount()>0) 
            {

                String id = icur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String  type=ncur.getString(ncur.getColumnIndex(Phone.TYPE));

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                {
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                                                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);     
                    Cursor typecur = ncr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null)          

                    while (pCur.moveToNext()&& typecur.moveToNext())
                    {

                        Log.d("names",name);
                        Log.d("types",type);
                        pCur.close();
                    } 
                }
            }
        }
    }
}

最佳答案

当你在阅读之前创建游标时。将光标位置移动到第一个。

while (cur.moveToNext()&& ncur.moveToNext()&& icur.getCount()>0) 

  /* move icur to first position then read */
  icur.moveFirst()

它必须工作

关于android.database.cursor 索引越界异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8064452/

相关文章:

Android:有关初始屏幕图像最佳尺寸的帮助

android - 类和 Activity 通过接口(interface)进行通信

java.lang.IndexOutOfBoundsException : Index 7, 大小:7

java - 如何抛出 ArrayIndexOutOfBoundsException?

java - IndexOutOfBoundsException: charAt: 0 >= length 0 在某些版本的 Android 上

python - 错误 : Command errored out with exit status 1 while installing requirements

java - 如何使用 Soot 在 APK 中创建和添加 Java 类?

android - 无法评价我的应用

Java 字符串用 "."(点)分割

java - 排序中的 ArrayIndexOutOfBounds