java - ArrayIndexOutOfBoundsException 错误...为什么?

标签 java android

我对这个有点迷茫。我如何获得 OutOfBounds?是否有大小限制(除了 sizeof (int))?

也许是因为多线程可以来到这里? UI 线程和服务线程?

java.lang.ArrayIndexOutOfBoundsException at kenyu73.realsignal.DatabaseWrapper.getSignalValues(DatabaseWrapper.java:137) at kenyu73.realsignal.DatabaseWrapper.getSignalValues(DatabaseWrapper.java:116) at kenyu73.realsignal.BarScaleGraph$buildGraphThread.drawGraph(BarScaleGraph.java:128) at kenyu73.realsignal.BarScaleGraph$buildGraphThread.execute(BarScaleGraph.java:94) at kenyu73.realsignal.BarScaleGraph$buildGraphThread.run(BarScaleGraph.java:74)

另外,我使用静态实例 调用此类方法。我在想线程正在争夺相同的变量???想法?

BarScaleGraph 类

ContentValues[] values = DatabaseWrapper.getInstance().getSignalValues(getContentResolver(), signal_type, false);

DatabaseWrapper 类

private static final DatabaseWrapper    instance    = new DatabaseWrapper();

// grab static instance so we only have one db wrapper
public static DatabaseWrapper getInstance() {
    return instance;
}

. . . .

public ContentValues[] getSignalValues(ContentResolver cr, int signal_type_id, boolean bGroupByLatLon) {

    String sWhere = "signal_type_id=" + signal_type_id;

    Cursor cursor;

    if (bGroupByLatLon) {
        cursor = cr.query(CONSTS.CONTENT_URI_GRP_LATLNG, null, sWhere, null, null);
    } else {
        cursor = cr.query(CONSTS.CONTENT_URI_LOGGER, null, sWhere, null, null);
    }

    ContentValues[] values = new ContentValues[cursor.getCount()];

    int count = 0;
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        do {
            values[count] = new ContentValues(); // <--- LINE 137
            values[count].put("signal_value", cursor.getInt(cursor.getColumnIndex("signal_value")));
            values[count].put("latitude", cursor.getInt(cursor.getColumnIndex("latitude")));
            values[count].put("longitude", cursor.getInt(cursor.getColumnIndex("longitude")));
            values[count].put("timestamp", cursor.getLong(cursor.getColumnIndex("timestamp")));
            values[count].put("network", cursor.getString(cursor.getColumnIndex("network")));

            count++;

        } while (cursor.moveToNext());
    }
    cursor.close();

    return values;
}

编辑:尝试这个 - 添加同步到实例

// grab static instance so we only have one db wrapper
public static synchronized DatabaseWrapper getInstance() {
    return instance;
}

最佳答案

唯一合理的答案是 cursor.getCount() 返回的数字低于您的 do..while 循环进行的循环次数。我没有在您的 do..while 循环逻辑中看到错误(尽管这是不寻常的逻辑;见下文)。

我的猜测是它是一个实时游标,而在您的循环运行时,其他东西正在删除添加相关行。找出答案的唯一真正方法是在代码中添加检测,这样您就可以看到 cursor.getCount() 返回了什么,每个循环开始时的 count 是什么迭代等

如果你真的不在乎为什么,只是想让它停止发生,你可以使用 List 代替:

public ContentValues[] getSignalValues(ContentResolver cr, int signal_type_id, boolean bGroupByLatLon) {

    String sWhere = "signal_type_id=" + signal_type_id;

    Cursor cursor;

    if (bGroupByLatLon) {
        cursor = cr.query(CONSTS.CONTENT_URI_GRP_LATLNG, null, sWhere, null, null);
    } else {
        cursor = cr.query(CONSTS.CONTENT_URI_LOGGER, null, sWhere, null, null);
    }

    List<ContentValues> values = new LinkedList<ContentValues>();
    ContentValues entry;

    while (cursor.moveToNext()) {
        entry = new ContentValues();
        entry.put("signal_value", cursor.getInt(cursor.getColumnIndex("signal_value")));
        entry.put("latitude", cursor.getInt(cursor.getColumnIndex("latitude")));
        entry.put("longitude", cursor.getInt(cursor.getColumnIndex("longitude")));
        entry.put("timestamp", cursor.getLong(cursor.getColumnIndex("timestamp")));
        entry.put("network", cursor.getString(cursor.getColumnIndex("network")));
        values.add(entry);
    }
    cursor.close();

    return values.toArray(new ContentValues[values.size()]);
}

(或实现该效果的代码。)

我使用了一个临时链表,所以我不关心 cursor.getCount 返回什么,完成后将其转换为数组。我还使用了更常见的循环游标习惯用法(因为游标刚好在第一行之前开始,while (cursor.moveToNext()) 是一种方便的循环方式),而不是(再次)我在你的 do..while 中发现了一个逻辑错误,但我喜欢 while (cursor.moveToNext()) 的简单和直接。

关于java - ArrayIndexOutOfBoundsException 错误...为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10010952/

相关文章:

java - 相同的 Java 源代码编译为二进制不同的类

java - 部分填充的数组 - 获取最小值和 toString

android - 在自定义通知中显示 HTML 内容

android - Mapbox setCameraPosition 未正确居中 View

android - 编译运行ApiDemos

android - ListView里面的两个LinearLayout和滚动问题

java - 调用gradle idea时如何将本地依赖放在第一位?

java - 您打算在什么项目上使用 Scala 编程语言?

java - 带 Jersey 的 HK2 注入(inject)管理器,使用 OpenJDK 11 升级

android - 将具有不同布局的项目动态添加到 ListView