当前位置: 首页 > 知识库问答 >
问题:

CursorIndexOutOfBoundsException:请求索引0,大小为0?

康赞
2023-03-14

我试图从我的sqlite数据库中获取一行。当我运行这行代码时:-

  RReckoner_info rReckoner_info = dbHandler.get_subcategory(1);
        Log.d("INFO : ", rReckoner_info.getCategory_name());

我得到以下错误:-

android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0

  public RReckoner_info get_subcategory(int id)
    {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_SUBCATEGORY_LIST, new String[]{KEY_NAME,KEY_DESC, KEY_CATEGORY_ID, KEY_CONTENT1,KEY_CONTENTTYPE1,KEY_CONTENT2,KEY_CONTENTTYPE2,KEY_CONTENT3,KEY_CONTENTTYPE3,
                KEY_CONTENT4,KEY_CONTENTTYPE4,KEY_CONTENT5,KEY_CONTENTTYPE5,KEY_CONTENT6,KEY_CONTENTTYPE6,KEY_ORDERID,KEY_STATUS ,KEY_UPDATED},KEY_NAME +"=?",
                new String[]{String.valueOf(id)},null,null, null,null);
        if (cursor !=null)
        {
            cursor.moveToFirst();
        }

        RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
                cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
                ,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
                cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
                Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));

        return rReckoner_info;

    }

有人能帮我解决这个问题吗?感谢任何帮助或建议。谢谢你。

共有3个答案

酆鸿哲
2023-03-14

没有由查询提取的记录,如CursorIndexOutOfBoundsExc0019所示:

指示光标超出范围的异常。

检查是否至少有记录:

 if (cursor !=null && cursor.getCount() > 0) {
     cursor.moveToFirst();


   // Code to extract data
}

或者您可以执行以下操作,因为如果没有记录,moveToFirst将返回false

如果游标为空,此方法将返回false。

if (cursor != null && cursor.moveToFirst()) {
    // Code to extract data
}
司空奕
2023-03-14

改为

    if (cursor !=null)
    {
        cursor.moveToFirst();

       RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
            cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
            ,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
            cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
            Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));

        return rReckoner_info;
   }
   return null;
郎健柏
2023-03-14

更改以下代码

发件人:

if (cursor !=null)
    {
        cursor.moveToFirst();
    }
RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
            cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
            ,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
            cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
            Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));

return rReckoner_info;

致:

if (cursor !=null && cursor.getCount() > 0)
    {
        cursor.moveToFirst();
        RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
            cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
            ,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
            cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
            Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));

    return rReckoner_info;
    }
return null;

如果没有找到我返回的null行,您可以根据您的要求更改最后一部分,但您必须检查光标的count,以避免出现CursorIndexOutOfBoundsException

 类似资料: