Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的应用程序不断崩溃,SQLite数据库泄漏错误_Java_Android_Sqlite_Android Sqlite - Fatal编程技术网

Java 我的应用程序不断崩溃,SQLite数据库泄漏错误

Java 我的应用程序不断崩溃,SQLite数据库泄漏错误,java,android,sqlite,android-sqlite,Java,Android,Sqlite,Android Sqlite,我目前正在从事一个大学项目,这是一个darts计算器应用程序,我不断收到数据库泄漏错误,尽管我认为我正在关闭所有的数据库和游标后,我使用它们。在程序开始时,我还收到一个“W/IdleConnectionHandler:删除一个从未存在过的连接”错误 我的logcat有以下错误: 05-01 08:33:47.801 9504-9581/? W/IdleConnectionHandler: Removing a connection that never existed! 05-01 08:43:

我目前正在从事一个大学项目,这是一个darts计算器应用程序,我不断收到数据库泄漏错误,尽管我认为我正在关闭所有的数据库和游标后,我使用它们。在程序开始时,我还收到一个“W/IdleConnectionHandler:删除一个从未存在过的连接”错误

我的logcat有以下错误:

05-01 08:33:47.801 9504-9581/? W/IdleConnectionHandler: Removing a connection that never existed!
05-01 08:43:46.141 9504-9581/? W/IdleConnectionHandler: Removing a connection that never existed!
以及:

我的数据库助手类如下所示:

package com.dartsapp.niall.darts_calculator_cs2048;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String database_name = "finishes.db";
private static final String table_name = "finishes_table";
//private static final String column1 = "score";
//private static final String column2 = "finish";
private static DatabaseHelper sInstance = null;

public static DatabaseHelper getInstance(Context context) {

    if (sInstance == null) {
        sInstance = new DatabaseHelper(context.getApplicationContext());
    }
    return sInstance;
}


private DatabaseHelper(Context context) {
    super(context, database_name, null, 1);
    //SQLiteDatabase db = this.getWritableDatabase();
    //db.close();
}

public Cursor getData(int playerScore) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select FINISH from "+table_name+" where SCORE = "+ playerScore,null);
    res.close();
    db.close();
    return res;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+ "(SCORE INTEGER, FINISH INTEGER)");
    String finish2 =
            "INSERT or replace INTO tbl_Contain (SCORE, FINISH) VALUES('2','D1')" ;
    db.execSQL(finish2);

// LONG list of INSERTs - OMITTED

    String finish170 =
            "INSERT or replace INTO tbl_Contain (SCORE, FINISH) VALUES('170', 'T20 T20 BULL')" ;
    db.execSQL(finish170);
    db.close();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + table_name);
    onCreate(db);
    db.close();
}

}

我在这里有很大的时间压力,所以非常感谢您的帮助`

在获取记录之前已关闭光标

您需要在关闭游标之前将其添加到代码中

res.moveToFirst(); //move Cursor to first record 
while (cur.isAfterLast() == false) {  // while Cursor not point to last record 
Log.v("YOURTAG",  cur.getString("yourString")); // get your data
//ToDo save your data
cur.moveToNext();
}
// ToDo return your data

现在您可以关闭光标了

您在获取记录之前关闭了光标

您需要在关闭游标之前将其添加到代码中

res.moveToFirst(); //move Cursor to first record 
while (cur.isAfterLast() == false) {  // while Cursor not point to last record 
Log.v("YOURTAG",  cur.getString("yourString")); // get your data
//ToDo save your data
cur.moveToNext();
}
// ToDo return your data

现在可以关闭光标了

如果要将光标返回给调用者,则无法关闭光标。让调用方在使用后关闭它。为什么要在OnCreate上执行查询,因为我希望数据在创建后立即放入数据库,但我对android studio缺乏经验,因此我不确定这是否正确。有没有更好的方法插入数据?您应该在需要时插入数据。如果要将光标返回给调用者,则无法关闭光标。让调用方在使用后关闭它。为什么要在OnCreate上执行查询,因为我希望数据在创建后立即放入数据库,但我对android studio缺乏经验,因此我不确定这是否正确。有没有更好的方法插入数据?您应该在需要时插入数据