我現在有一個循環內檢查一個表,如果一個隨機值存在的數據庫,然後將它,如果它沒有。如果隨機值確實存在,它會得到一個新的隨機值並再次嘗試。它執行36次,然後刪除表格。安卓:遊標不關閉
我的這個代碼位:
public void loop() {
random(); //Simple RNG
try{
id = db.getAllEntries();
Log.d(TAG, "ran = " + ran + " i = " + i + " id = " + id);
db.getTest(ran);
loop();
}
catch(Exception ex){
populatePeices(ran); //Makes ImageButtons have images
}
}
我的數據庫的方法是:
public void getTest(int ran) {
Cursor cursor = db.rawQuery("SELECT imageName FROM tblMusicHall WHERE imageName = " + ran, null);
cursor.moveToNext();
Log.d(TAG, "Cursor " + cursor.getString(0));
cursor.close();
}
日誌是實時觀看的過程。事情是,當它拋出「Cursor not closed」異常時,它是完全隨機的。有時我會看着它經歷所有36次迭代,而不是拋出一次。其他時候它會經歷循環的所有36次迭代,然後拋出36個異常。我的頭腦很難受!
而且我一直在使用startManagingCursor()嘗試,但我希望光標自己的控制。
我的錯誤,我繼續收到如下。如果有人有任何想法,請分享!
04-18 07:59:04.280: ERROR/Cursor(31526): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.tallgrass.xxxxxxxxx/databases/xxxxxxxxxx,table = null, query = SELECT imageName FROM tblMusicHall WHERE imageName = 4
04-18 07:59:04.280: ERROR/Cursor(31526): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
04-18 07:59:04.280: ERROR/Cursor(31526): at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
04-18 07:59:04.280: ERROR/Cursor(31526): at com.tallgrass.xxxxxxxx.DBAdapter.getTest(DBAdapter.java:135)
04-18 07:59:04.280: ERROR/Cursor(31526): at com.tallgrass.xxxxxxxx.Play.loop(Play.java:45)
04-18 07:59:04.280: ERROR/Cursor(31526): at com.tallgrass.xxxxxxxx.Play.onCreate(Play.java:31)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.widget.TabHost.addTab(TabHost.java:213)
04-18 07:59:04.280: ERROR/Cursor(31526): at com.tallgrass.xxxxxxxx.Main.onCreate(Main.java:36)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.os.Looper.loop(Looper.java:123)
04-18 07:59:04.280: ERROR/Cursor(31526): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-18 07:59:04.280: ERROR/Cursor(31526): at java.lang.reflect.Method.invokeNative(Native Method) 04-18 07:59:04.280: ERROR/Cursor(31526): at java.lang.reflect.Method.invoke(Method.java:521)
04-18 07:59:04.280: ERROR/Cursor(31526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-18 07:59:04.280: ERROR/Cursor(31526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-18 07:59:04.280: ERROR/Cursor(31526): at dalvik.system.NativeStart.main(Native Method)
非常感謝! IDK有什麼不同,但它現在起作用了!編輯:其實我現在看到。當它正在經歷循環時,它可能會在moveToNext()或getString()上拋出異常,並且從未得到改變來關閉它。 – 2011-04-18 08:53:15
是的,你說得對。總是從'finally'節調用'Cursor.close()'這樣的方法。它會幫助你避免資源泄漏。 – Michael 2011-04-18 09:34:44