开发者

Android: Cursor not closing

开发者 https://www.devze.com 2023-02-25 08:04 出处:网络
I currently have a database that within a loop checks a table if a random value exists and then adds it if it doesn\'t. If the random value does exists it just gets a new random and tries again. It do

I currently have a database that within a loop checks a table if a random value exists and then adds it if it doesn't. If the random value does exists it just gets a new random and tries again. It does this 36 times and then deletes the table.

My bit of code for this is:

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
            }

    }

My database method is:

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

The log is to watch the process in real time. The thing is, It is COMPLETELY random when it will throw the "Cursor not closed" exception. Sometimes I will watch it go through all 36 iterations and not throw one. Other times it will go through all 36 iterations of the loop then throw 36 exceptions. My mind is boggled!

Also I have tried using startManagingCursor() but I want control of the cursor myself.

My error that I continue to receive is below. If anyone has any thoughts please share!

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.SQLiteDa开发者_如何学Gotabase.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)


Use getActivity().startManagingCursor(cursor); right after the initialization of Cursor if you are using Cursor in your Activity, startManagingCursor(cursor) will manage your cursor, and cursor.close() in Db Adapter class if you are using Cursor in DB adapter class.


If an exception is thrown in getTest() method, then a cursor may be not closed. Try changing this method to something like:

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();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号