完成游标错误

时间:2013-01-16 19:13:39

标签: android

我试图关闭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)

2 个答案:

答案 0 :(得分:0)

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

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

也许这就是问题的根源。

答案 1 :(得分:0)

从您的交换机案例中删除 db.close();

在onDestroy()中执行此操作。