所以我只是在一个我已经开发了一段时间的应用程序上完成所有的事情。我在控制台中遇到的唯一错误是 DatabaseObjectNotClosedException。它不会导致应用程序关闭或崩溃或任何事情,但我担心这一点,因为我已经读到未关闭的光标可能是一个大问题。
所以这里是错误信息:
05-28 20:40:01.598: E/Database(655): android.database.sqlite.DatabaseObjectNotClosedException: 应用程序没有关闭在此处打开的游标或数据库对象
这是它所指的代码:
datasource = new ProgressDataSource(this);
datasource.open();
这是 ProgressDataSource 的代码:
package com.gauv.myapp;
import java.util.Arrays;
import java.util.Collections;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.jjoe64.graphview.GraphView.GraphViewData;
public class ProgressDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public ProgressDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public GraphViewData[] fetchProgress(long dayExerciseDataID) {
Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_1RM + " " +
"from " + MySQLiteHelper.TABLE_LOGS + " " +
"where " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + "='" + dayExerciseDataID + "' " +
"order by " + MySQLiteHelper.COLUMN_DATE + " desc limit 30", null);
GraphViewData[] data = new GraphViewData[cursor.getCount()];
Long[] onerms = new Long[cursor.getCount()];
int i = 0;
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
onerms[i] = cursor.getLong(0);
i++;
cursor.moveToNext();
}
Collections.reverse(Arrays.asList(onerms));
int y = 1;
for (int x = 0; x < onerms.length; x++) {
data[x] = new GraphViewData(y, onerms[x]);
y++;
}
return data;
}
public String fetchProgressCount(long dayExerciseDataID) {
Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_1RM + " " +
"from " + MySQLiteHelper.TABLE_LOGS + " " +
"where " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + "='" + dayExerciseDataID + "' " +
"order by " + MySQLiteHelper.COLUMN_DATE + " desc limit 12", null);
return String.valueOf(cursor.getCount());
}
}
我只需要添加cursor.close()
到 fetchProgress 函数吗?