【问题标题】:Android Studio - Value must be ≥ 0Android Studio - 值必须≥ 0
【发布时间】:2021-11-02 06:36:59
【问题描述】:

我在 Android Studio 中遇到与光标有关的错误。

我的代码中有以下行

String data = cursor.getString(cursor.getColumnIndex(columnIndex));

columnIndex 正在被传递到方法中。

这部分 cursor.getColumnIndex(columnIndex) 产生以下错误

值必须≥0

它在我的 DBHelper 类和我的回收器适配器中发生,当它也使用游标时。

它以红色显示为错误,但应用程序仍然可以正常构建和运行。

有什么想法吗?

感谢您的帮助。

21 年 9 月 22 日更新

我正在根据要求添加一些代码,以及我是如何解决这个错误的。不确定这是否是最好的方法。

所以我使用的方法是这样的......

public String getTripInfo(String tableName, int tripNo, String columnIndex){
    String data = "";

    // Select all query
    String selectQuery = "SELECT * FROM " + tableName + " WHERE " + TRIP_DETAILS_TRIP_NUMBER + "=" + tripNo;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // Looping through all rows and adding to list
    if(cursor.moveToFirst()){
        do{
            data = cursor.getString(cursor.getColumnIndex(columnIndex));
        } while(cursor.moveToNext());
    }

    // Closing connections
    cursor.close();
    db.close();

    //Returning number plates
    return data;
}

错误出现在 do while 循环中。红色部分为“cursor.getColumnIndex(columnIndex))”

我解决此错误的方法是改用以下代码

public String getTripInfo(String tableName, int tripNo, String columnIndex){
    String data = "";

    // Select all query
    String selectQuery = "SELECT * FROM " + tableName + " WHERE " + TRIP_DETAILS_TRIP_NUMBER + "=" + tripNo;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // Looping through all rows and adding to list
    if(cursor.moveToFirst()){
        do{
            int index = cursor.getColumnIndex(columnIndex);
            data = cursor.getString(index);
        } while(cursor.moveToNext());
    }

    // Closing connections
    cursor.close();
    db.close();

    //Returning number plates
    return data;
}

【问题讨论】:

  • 这意味着 columnIndex 有时是负数,请查看传递 columnIndex 的方法。表列索引不得为负数或大于最后一列索引。
  • 感谢您的回复。所以根据文档,可以返回-1,如下所示。所以我仍然不确定我能做些什么来解决这个错误。你有机会再解释一下吗? getColumnIndex(String columnName) 返回给定列名的从零开始的索引,如果该列不存在,则返回 -1。
  • cursor.getColumnIndex 采用字符串参数,这是您获取其列索引的表名。因此,您可以传递正确的参数,如您所见。你能分享与该问题相关的完整代码吗?
  • 请提供足够的代码,以便其他人更好地理解或重现问题。
  • 只需使用:cursor.getColumnIndexOrThrow(...)。最初的问题是 lint 问题 - 运行 ./gradlew lint 将显示这一点。这必须在最新的 AS 版本中发生 - 北极狐 2020.3.1 补丁 2 - 更新到此补丁版本后代码更改为零,因此我无法再构建项目 - 如果您查看更改日志,它提到了一个修复lint 检查:androidstudio.googleblog.com/2021/09/… & issuetracker.google.com/issues/197146610?pli=1 可能是这样的......

标签: android android-cursor


【解决方案1】:

我遇到了这样的错误。
我的解决方案:将方法 getColumnIndex 更改为 getColumnIndexOrThrow

【讨论】:

    【解决方案2】:

    问题是cursor.getColumnIndex() 可以返回-1,您将它作为直接参数传递,并且游标getter 需要列索引gte 0。getter 的参数用@IntRange(from = 0) 注释,这就是lint 的原因将其标记为错误。

    因此,即使 可能已经构建了您的项目,这样它就不会产生无效的列索引,但这种可能存在的事实就是 lint 标记它的原因。

    您的代码修订只是避免了这个问题。 gte 0 最好使用cursor.getColumnIndexOrThrow() 或测试index

    由于您对项目的了解比 lint 多,因此您可以侥幸成功,但这并不是最佳实践。

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多