因此,我只是最終確定了一段時間我一直在研究的應用程序上的所有內容。我在控制檯中遇到的唯一錯誤是DatabaseObjectNotClosedException。它不會導致應用程序關閉或崩潰或任何事情,但我擔心這一點,因爲我已經讀過一個未封閉的光標可能是一個大問題。DatabaseObjectNotClosedException在控制檯中
因此,這裏的錯誤消息:
05-28 20:40:01.598:E /數據庫(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函數?
我只是將它添加到的onPause功能在我的活動,它解決了問題。 – scarhand