我目前有一个数据库,在循环内检查表是否存在随机值,然后如果不存在则添加它。如果随机值确实存在,则只获取新的随机值并再次尝试。它这样做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)
答案 0 :(得分:4)
如果在Activity中使用Cursor,则在Cursor初始化后立即使用getActivity().startManagingCursor(cursor);
,startManagingCursor(cursor)将管理光标,如果在DB中使用Cursor,则在Db Adapter类中使用cursor.close()
适配器类。
答案 1 :(得分:2)
如果在getTest()
方法中抛出异常,则可能未关闭游标。尝试将此方法更改为:
public void getTest(int ran) {
Cursor cursor;
try {
cursor = db.rawQuery("SELECT imageName FROM tblMusicHall WHERE imageName = " + ran, null);
cursor.moveToNext();
Log.d(TAG, "Cursor " + cursor.getString(0));
} finally {
if (cursor != null) {
cursor.close();
}
}
}