0

我试图关闭 db 中的游标,但随后它说对已关闭的游标对象的无效调用,因为我得到了一个游标作为回报......

  Cursor getDB(String date) {
    int idate = Integer.parseInt(date.substring(0, 2)) * 1000000
            + Integer.parseInt(date.substring(3, 5)) * 10000
            + Integer.parseInt(date.substring(6));

    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getReadableDatabase();

    Cursor cursor = ourDatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE
            + " WHERE " + KEY_DATE + " = " + idate,// +
                                                    // " ORDER BY "
                                                    // +
                                                    // KEY_NAME
                                                    // +
                                                    // " ASC",
            null);
    if (cursor != null && cursor.getCount() > 0)
        return cursor;

    return null;
}

&这就是我如何使用我得到的光标作为回报

我将它们初始化为

adapter = new MyAdapter1(Day_Book.this, ourCursor, false);
adapter2 = new MyAdapter2(Day_Book.this, ourCursor2, false);
case R.id.bShowDB:
        db = new DB(Day_Book.this);
        ourCursor = db.getDB(et1.getText().toString());
        adapter.changeCursor(ourCursor);
        adapter2.changeCursor(ourCursor);
        //ourCursor.close();
        db.close();
        lv1.setAdapter(adapter);
        lv2.setAdapter(adapter2);
        //ourCursor.close();
        break;

这是我得到的错误..

01-17 00:12:41.134: E/Cursor(8724): Finalizing a Cursor that has not been deactivated or closed. database = /mnt/sdcard/JournalDB.db, table = null, query = SELECT * FROM    Contains_all_the_entries_of_an_year_of_a_particular_company WHERE  date = 17012013
01-17 00:12:41.134: E/Cursor(8724): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-17 00:12:41.134: E/Cursor(8724):     at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:212)
01-17 00:12:41.134: E/Cursor(8724):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
01-17 00:12:41.134: E/Cursor(8724):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1454)
01-17 00:12:41.134: E/Cursor(8724):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1424)
01-17 00:12:41.134: E/Cursor(8724):     at com.balance.start.JournalDB.getDayBook(DB.java:232)
01-17 00:12:41.134: E/Cursor(8724):     at com.balance.start.Day_Book.onClick(Book.java:93)
01-17 00:12:41.134: E/Cursor(8724):     at android.view.View.performClick(View.java:2408)
01-17 00:12:41.134: E/Cursor(8724):     at android.view.View$PerformClick.run(View.java:8816)
01-17 00:12:41.134: E/Cursor(8724):     at android.os.Handler.handleCallback(Handler.java:587)
01-17 00:12:41.134: E/Cursor(8724):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 00:12:41.134: E/Cursor(8724):     at android.os.Looper.loop(Looper.java:123)
01-17 00:12:41.134: E/Cursor(8724):     at     android.app.ActivityThread.main(ActivityThread.java:4633)
01-17 00:12:41.134: E/Cursor(8724):     at java.lang.reflect.Method.invokeNative(Native Method)
01-17 00:12:41.134: E/Cursor(8724):     at java.lang.reflect.Method.invoke(Method.java:521)
01-17 00:12:41.134: E/Cursor(8724):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-17 00:12:41.134: E/Cursor(8724):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-17 00:12:41.134: E/Cursor(8724):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

嗯,您为两个不同的适配器设置了两次相同的光标:

adapter.changeCursor(ourCursor);
adapter2.changeCursor(ourCursor);

也许这就是问题的根源。

于 2013-01-16T19:22:36.157 回答
0

删除db.close(); 从你的开关盒。

在 onDestroy() 中执行此操作。

于 2013-01-16T19:28:30.950 回答