2013-02-05 66 views
0

我試圖從SQLiteDatabase的rawQuery,但不斷得到一個遊標java.lang.IllegalStateException。此異常精確:沒有得到一個開放的Sqlite數據庫

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; 
} 

我所做的布爾檢查wheather數據庫是開放的或不。它返回false。

任何人都可以幫我爲什麼?

完全錯誤日誌:

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 
+0

'在frisbeergolf.database.DBHandler.getAllPlayersInGame(DBHandler.java:175)'要確定,其是線175? – Sam

+0

我覺得有點愚蠢,因爲我自己找到了答案,而且很簡單。我在我的getNumberOfPlayersInGame方法中調用並**關閉**一個數據庫。我需要解決的所有問題都是這些行的順序:int count = this.getNumberOfPlayersInGame(game); SQLiteDatabase db = this.getReadableDatabase();' – oht

+0

您應該將其作爲未來訪問者的答案發布,很高興您能夠實現它! – Sam

回答

0

這一直是我自己的錯。

我叫了,已關閉我的this.getNumberOfPlayersInGame(game);方法中的數據庫。因此也關閉了新開的那個。

溶液只是爲了這些行切換到這個順序和所有工作得很好

int count = this.getNumberOfPlayersInGame(game); 
    SQLiteDatabase db = this.getReadableDatabase(); 
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的代碼問題。請粘貼整個代碼,或嘗試分析我發佈的代碼以查看不同之處。

相關問題