【问题标题】:Cursor Index Out of Bounds exception光标索引超出范围异常
【发布时间】:2017-06-09 05:47:51
【问题描述】:

我不断收到此错误消息:“原因:android.database.CursorIndexOutOfBoundsException:请求索引 0,大小为 0”在我的代码中。

我已经引用了以下链接,但没有一个有效: Android cursor out of bounds exception

Cursor index out of bounds

Cursor Index Out Of Bounds Error Android?

cursor index out of bounds exception

  public Product getProduct(int id) {
    db = this.getReadableDatabase();

    Cursor cursor = db.query(
            TABLE_NAME,
            null,
            COLUMN_ID + " = ?",
            new String[] { Integer.toString(id)},
            null, null, null, null);

    Product product = new Product();

    cursor.moveToFirst();
    if (cursor != null) {
        product.setID(cursor.getInt(0));
        product.setName(cursor.getString(1));
        product.setSize(cursor.getDouble(2));
        product.setHp(cursor.getDouble(3));
        product.setCategory(cursor.getString(4));
        product.setPowerType(cursor.getString(5));
        product.setExplosionProof(cursor.getInt(6));
        product.setRPM(cursor.getInt(7));
        product.setBypassPSI(cursor.getInt(8));
        product.setGPM(cursor.getInt(9));
        product.setPrice(cursor.getDouble(10));
        cursor.close();
    }

    db.close();
    return product;
}

这是我的常量:

private static final String TABLE_NAME = "Products";
private static final String COLUMN_ID = "'_id'";

如果有任何帮助,我将不胜感激。

现在,它不会进入 if 循环。我直接从另一个堆栈溢出问题中得到了该查询模型。

【问题讨论】:

  • 如果您的结果为空,就会发生这种情况。你能显示完整的堆栈跟踪吗
  • 在调用cursor.moveToFirst() 的行后面加上if (cursor != null) 有什么意义?
  • 添加检查 if(cursor.moveToFirst()) 而不是 if (cursor != null)
  • 当我执行 if(cursor.moveToFirst()) 时,它永远不会进入 if 循环
  • 你应该使用if(cursor.moveToNext())

标签: java android sqlite android-sqlite


【解决方案1】:

光标很可能是空的,因此您需要确定原因。可能是没有基础数据,也可能是游标 where 子句排除了所有数据。根据您提供的信息,无法确定原因。

我建议删除 where 子句,看看这是否重要。

即而不是

    Cursor cursor = db.query(
        TABLE_NAME,
        null,
        COLUMN_ID + " = ?",
        new String[] { Integer.toString(id)},
        null, null, null, null);

使用:-

    Cursor cursor = db.query(
        TABLE_NAME,
        null,
        null,
        null,
        null, null, null, null);

这将返回游标中的所有行,如果可行,则问题出在 where 子句(传递给 db.query 的第 3 和第 4 个参数)。如果它不能解决问题,那么很可能是表本身没有行。

您可以使用 :-

绕过 CursorIndexOutOfBoundsException
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        product.setID(cursor.getInt(0));
        product.setName(cursor.getString(1));
        product.setSize(cursor.getDouble(2));
        product.setHp(cursor.getDouble(3));
        product.setCategory(cursor.getString(4));
        product.setPowerType(cursor.getString(5));
        product.setExplosionProof(cursor.getInt(6));
        product.setRPM(cursor.getInt(7));
        product.setBypassPSI(cursor.getInt(8));
        product.setGPM(cursor.getInt(9));
        product.setPrice(cursor.getDouble(10));
    } else {
        // setup to return a product not found
    }
    cursor.close();

但是,这并不能解决游标未返回任何行的根本问题。

您可以使用类似以下的内容来获取有关光标的更多信息(根据上面的更改选择所有行的光标运行)。这应该适用于任何光标:-

    int rowcount = cursor.getCount();
    String colnames = "";
    for (String columns : cursor.getColumnNames()) {
        colnames = colnames +
                "Column Index=" +
                Integer.toString(cursor.getColumnIndex(columns)) +
                " Name=>" +
                columns +
                "<  ";
    }
    Log.d("DBCHK","Table has " + Integer.toString(rowcount) + " rows. With columns:- " + colnames);
    String values;
    while (cursor.moveToNext()) {
        values = "";
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            values = values +
                    "Column = " +
                    cursor.getColumnName(i) +
                    " Index=" +
                    Integer.toString(i) +
                    " Value=" + cursor.getString(i) + ": ";
        }
        Log.d("DBCHK",values);
    }

这将按照以下方式将数据写入日志:-

D/DBCHK: Table has 2 rows. With columns:- Column Index=0 Name=>_id<  Column Index=1 Name=>myfloat<  
D/DBCHK: Column = _id Index=0 Value=null: Column = myfloat Index=1 Value=56.7897: 
D/DBCHK: Column = _id Index=0 Value=null: Column = myfloat Index=1 Value=87.7655: 

每行会有一行,前面有一行列出行数和列是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多