我正在使用包含DataPoint[]
数组的GraphView
库代码。
我的代码还包含一个名为olddb 的数据库,我用它在下面的代码中插入数组长度。
前 2 个 for 循环用于将数据库中的值添加到数组中,第三个 for 循环用于从数组 arrayWeight 和 arrayId 中获取值并将它们添加到 DataPoint[] 数组中。
虽然似乎出了问题,但在我运行程序后它就崩溃了,任何人都可以找出导致应用程序崩溃的原因吗?
private DataPoint[] getDataPoint() {
if (olddb.check()) {
List<oldDetails> details = olddb.getDetails();
double[] arrayWeight = new double[olddb.getDetailsCount()];
int[] arrayId = new int[olddb.getDetailsCount()];
for (oldDetails cn : details) { //Adding weights and id of all time to an array.
double num = cn.getWeight();
int id = cn.getId();
for (int i = arrayWeight.length; i > 0; i--) {
arrayWeight[i] = num;
arrayId[i] = id;
}
}
DataPoint[] dp = new DataPoint[olddb.getDetailsCount()];
for (int i = 0; i < arrayId.length; i++) {
for (int j = 0; j < arrayWeight.length; j++)
dp[i] = new DataPoint(i, arrayWeight[j]);
}
return dp;
}
else {
DataPoint[] dp = new DataPoint[]{
new DataPoint(0, 0)
};
return dp;
}
}
以下是 DataPoint
数组的工作代码示例:
private DataPoint[] getDataPoint(){
DataPoint[] dp = new DataPoint[]{
new DataPoint(0,1),
new DataPoint(2,5),
new DataPoint(5,5),
new DataPoint(7,4)
};
return dp;
}
编辑: 这是错误消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.none.myapplication, PID: 3881
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.none.myapplication/com.none.myapplication.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM oldDetails
最佳答案
您可以通过将所有 3 次调用替换为 olddb.getDetailsCount()
来修复此问题
与
int count = olddb.getDetailsCount();
然后:
double[] arrayWeight = new double[count];
int[] arrayId = new int[count];
DataPoint[] dp = new DataPoint[count];
但是您需要了解这个问题:
java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteQuery:SELECT * FROM oldDetails
是错误,发生在 DatabaseHandlerOld.getDetailsCount
第 141 行内
关于java - 将值添加到 DataPoint 数组中导致应用程序在运行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41403318/