Android SQL数据库应用实践 “问题点”“疑难点”“解析”

本文介绍了一种在Android应用中使用SQL查询时遇到的CursorIndexOutOfBoundsException异常及其解决方案。该异常通常发生在尝试从Cursor中获取数据时,Cursor指针未正确定位。文章提供了具体的代码示例及修正方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用 Android SQL 数据库时,遇到的问题:

场景1:Android SQL查询后,获取到Cursor并查询数据;遇到以下问题:"android.database.CursorIndexOutOfBoundsException: Index -1 requested"

D/Demo    (22249): [ContackPickerActivity] onItemClick::cursor.getCount()=70; position=1
D/Demo    (22249): [ContackPickerActivity] outUri.toString()=content://com.android.contacts/contacts/2
D/Demo    (22249): [ContactPickerTestActivity] onActivityResult::requestCode=1000
D/Demo    (22249): [ContactPickerTestActivity] onActivityResult::PICK_CONTACK running...
D/Demo    (22249): [ContactPickerTestActivity] onActivityResult::contackData=content://com.android.contacts/contacts/2
D/Demo    (22249): [ContactPickerTestActivity] ERROR is detected...android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

源代码如下:

        switch (requestCode) {
            case PICK_CONTACK:
                if (resultCode == Activity.RESULT_OK) {
                    LogUtil.d(TAG, "onActivityResult::PICK_CONTACK running...");
                    Uri contactData = data.getData();
                    LogUtil.d(TAG, "onActivityResult::contackData="
                            + contactData.toString());
                    Cursor cursor = getContentResolver().query(contactData,
                            null, null, null, null);
                    if (cursor != null) {
                        String name;
                        try {
                            name = cursor
                                    .getString(cursor
                                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
                            cursor.close();
                            TextView tv = (TextView) findViewById(R.id.selected_contact_textview);
                            tv.setText(name);
                        } catch (Exception e) {
                            e.printStackTrace();
                            LogUtil.d(TAG,
                                    "ERROR is detected..." + e.toString());
                        }
                    }
                }
                break;
            default:
                break;
        }

问题出在以下点:

name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));

在使用query()查询时,返回结果描述如下:

     * @return A Cursor object, which is positioned before the first entry, or null

Cursor指针指向的是查询结果的前一位置,在这之后调用的getColumnIndex()出现超出“边界”的异常。

解决办法:调用moveToFirst()即可

if (cursor != null && cursor.moveToFirst()) {
    String name;
    try {
        name = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
        cursor.close();
        TextView tv = (TextView) findViewById(R.id.selected_contact_textview);
        tv.setText(name);
    } catch (Exception e) {
        e.printStackTrace();
        LogUtil.d(TAG,
                "ERROR is detected..." + e.toString());
    }
}

转载于:https://www.cnblogs.com/CVstyle/p/6388136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值