0

我正在尝试从 SQLiteDatabase rawQuery 获取游标,但不断获取java.lang.IllegalStateException. 准确地说,这个例外:

    02-05 19:30:25.100: E/AndroidRuntime(338): java.lang.RuntimeException: Unable to start activity ComponentInfo{frisbeergolf.gui/frisbeergolf.gui.Hole}: java.lang.IllegalStateException: attempt to acquire a reference on a close SQLiteClosable

(下面附上完整的日志)

这是我在 SQLiteOpenHelper 类中的方法:

    public int[] getAllPlayersInGame(Game game){
    SQLiteDatabase db = this.getReadableDatabase();
    int count = this.getNumberOfPlayersInGame(game);
    int[] playerIDs = new int[count];

    Boolean b = db.isOpen();

    Cursor cursor = null;
    cursor = db.rawQuery("SELECT playerID FROM " + gamesTable + " WHERE gameID = " + game.getGameId(), null);

    cursor.moveToFirst();

    for (int i = 0; i < count; i++) {
        playerIDs[i] = cursor.getInt(0);
        cursor.moveToNext();
    }
    cursor.close();
    db.close();

    return playerIDs;
}

我做了布尔值来检查数据库是否打开。它返回错误。

谁能帮我看看为什么?

完整的错误日志:

    02-05 19:30:25.080: W/dalvikvm(338): threadid=3: thread exiting with uncaught      exception (group=0x4001b188)
    02-05 19:30:25.080: E/AndroidRuntime(338): Uncaught handler: thread main exiting due to uncaught exception
    02-05 19:30:25.100: E/AndroidRuntime(338): java.lang.RuntimeException: Unable to start activity ComponentInfo{frisbeergolf.gui/frisbeergolf.gui.Hole}: java.lang.IllegalStateException: attempt to acquire a reference on a close SQLiteClosable
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.os.Handler.dispatchMessage(Handler.java:99)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.os.Looper.loop(Looper.java:123)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.app.ActivityThread.main(ActivityThread.java:4363)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at java.lang.reflect.Method.invokeNative(Native Method)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at java.lang.reflect.Method.invoke(Method.java:521)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at dalvik.system.NativeStart.main(Native Method)
    02-05 19:30:25.100: E/AndroidRuntime(338): Caused by: java.lang.IllegalStateException: attempt to acquire a reference on a close SQLiteClosable
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:31)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:56)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1221)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1194)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at frisbeergolf.database.DBHandler.getAllPlayersInGame(DBHandler.java:175)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at frisbeergolf.database.DBHandler.getProgressAtHole(DBHandler.java:226)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at frisbeergolf.game.Game.getProgressAtHole(Game.java:63)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at frisbeergolf.gui.Hole.onCreate(Hole.java:31)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    02-05 19:30:25.100: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    02-05 19:30:25.100: E/AndroidRuntime(338):  ... 11 more
4

2 回答 2

0

如果您发布了代码,我将能够使用您的代码给出一个示例,但我会使用我自己的一些代码,希望我们可以解决您的问题。

https://bitbucket.org/XatikGroup/droiddraw/src/8aee6ffc4a7d089714c5003e32f29652eafd79ef/DroidDrawClient/src/com/xatik/app/droiddraw/client/databases/CommandsDbAdapter.java?at=master

当你打电话时,我不完全确定“这是”是什么

this.getReadableDatabase();

如果你看看我的课程,我在 DbAdapter 中有一个私有静态类。这个类有助于打开数据库。我认为这就是代码中问题的来源。

在我需要数据的活动中,我将创建一个 CommandsDbAdapter 对象,然后调用 open() 函数。

CommandsDbAdapter cmdDbAdapter = new cmdDbAdapter(this);
cmdDbAdapter.open();
List<Command> commands = cmdDbAdapter.fetchCommandsForScript(1);
cmdDbAdapter.close();

我假设扩展 SQLiteOpenHelper 的任何类都存在代码问题。请粘贴整个代码,或尝试分析我发布的代码,看看有什么不同。

于 2013-02-05T21:04:21.307 回答
0

一直都是我自己的错。

我打电话,并在我的方法中关闭了一个数据库。this.getNumberOfPlayersInGame(game);因此也关闭了新开的一个。

解决方案就是将这些行切换到这个顺序,一切都很好

    int count = this.getNumberOfPlayersInGame(game);
    SQLiteDatabase db = this.getReadableDatabase();
于 2013-02-07T19:52:49.427 回答