【问题标题】:android.database.cursor index out of bound of exception?android.database.cursor 索引超出异常范围?
【发布时间】:2011-12-25 06:05:13
【问题描述】:

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

android.database.CursorIndexOutOfBoundsException:索引 -1 请求大小为 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();
                    } 
                }
            }
        }
    }
}

【问题讨论】:

  • 为什么要制作三个单独的游标?
  • 因为使用了三种不同的投影。我尝试使用一种但出现编译时错误。

标签: android indexoutofboundsexception


【解决方案1】:

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

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

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

它必须工作

【讨论】:

  • cur.moveToNext() 将为您执行此操作,它将充当 cur.moveToFirst()。
【解决方案2】:

经过几天的搜索,我终于得到了答案..类型和名称存储在 DATA2 列下的数据表中,因此我查询了 data.content_uri 并获得了 DATA2 列的索引,它工作正常......我得到了名称和类型。

【讨论】:

    【解决方案3】:

    你不应该创建三个不同的游标和上下文解析器,使用相同的,但在循环中设置条件以获得你想要的,我给你一个我自己写的方法。 这将为您提供手机中具有电话号码的所有联系人,您将了解实现自己的想法:

    public ArrayList<Contact> getSMSContacts(Context context){
        ArrayList<Contact> contacts = new ArrayList<Contact>();
        ContentResolver cr = context.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " asc");
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Contact c = new Contact();
                    c.setId(Integer.parseInt(id));
                    c.setName(name);
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                            new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String number = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        c.setNumber(number);
                    } 
                    pCur.close();
                    contacts.add(c);
                }
            }
        }
        cur.close();
        return contacts;
    }
    

    【讨论】:

    • 您在这里只获得存储在联系人列中的联系人。对于类型,它类似于 Cursor ncur=ncr.query(ContactsContract.Data.CONTENT_URI, projection1, null, null,null);作为类型在 Data column.same as contact_id get store in rawcontactid column.besides 我使用单个光标对象获取运行时异常并将投影传递给它。
    • 就像我告诉你的那样,你会看到我的实现的想法,顺便说一句,它工作正常!你说的类型是什么意思?除了联系人姓名,你还想要什么。
    • 字符串类型 = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    猜你喜欢
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多