【问题标题】:java.lang.IllegalStateException: attempt to re-open an already-closed object(Tried closing )java.lang.IllegalStateException:尝试重新打开已经关闭的对象(尝试关闭)
【发布时间】:2013-03-11 12:16:47
【问题描述】:
public int getRecordsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        if(cursor != null && !cursor.isClosed()){
            cursor.close();
        }   
        // return count

        return cursor.getCount();
    }

我正在尝试获取数据库中的记录总数,但每次使用 java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM login) 时数据库都会崩溃。请帮我解决错误

  03-05 22:23:14.208: E/AndroidRuntime(4988): FATAL EXCEPTION: main
    03-05 22:23:14.208: E/AndroidRuntime(4988): java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT  * FROM login) 
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.ecomm.android.sqlite.DatabaseHandler.getRecordsCount(DatabaseHandler.java:123)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.ecomm.android.LaunchActivity.DataBaseImplementation(LaunchActivity.java:120)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.ecomm.android.LaunchActivity.onClick(LaunchActivity.java:98)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.view.View.performClick(View.java:2408)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.view.View$PerformClick.run(View.java:8816)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.os.Handler.handleCallback(Handler.java:587)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.os.Handler.dispatchMessage(Handler.java:92)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.os.Looper.loop(Looper.java:123)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at java.lang.reflect.Method.invoke(Method.java:521)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at dalvik.system.NativeStart.main(Native Method)
    03-05 22:23:15.608: I/binder_sample(4988): [android.app.IActivityManager,2,1395,com.ecomm.android,100]
    03-05 22:23:15.608: I/binder_sample(4988): Unknown binary event type 110
    03-05 22:23:15.608: I/binder_sample(4988): Binary log entry conversion failed

【问题讨论】:

    标签: android sqlite android-sqlite


    【解决方案1】:

    您应该在关闭光标之前调用getCount()

    移动:

    if(cursor != null && !cursor.isClosed()){
        cursor.close();
    } 
    

    下面:

     cursor.getCount();
    

    像这样:

    public int getRecordsCount() {
        int count = 0;
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
    
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
    
    
    
        if(cursor != null && !cursor.isClosed()){
            count = cursor.getCount();
            cursor.close();
        }   
        return count;
    }
    

    【讨论】:

      【解决方案2】:

      java.lang.IllegalStateException: 尝试重新打开一个已经关闭的

      您的错误被抛出,因为您在 Cursor 上调用 cursor.getCount() 并且您已经关闭并且这是不允许的。

      所以要么尝试使用 try-finally 块,在 finally 块中关闭你的 Cursor 或将 cursor.getCount() 分配给 int 值并立即关闭 Cursor

      但我建议您使用第一种方法:

      public int getRecordsCount() {
         String countQuery = "SELECT * FROM " + TABLE_LOGIN;
         SQLiteDatabase db = this.getReadableDatabase();
         Cursor cursor = db.rawQuery(countQuery, null);
         int count = 0;
         try {
            if (cursor.moveToFirst())) {
               count = cursor.getCount();
            }
            return count;
         }
         finally {
            if (cursor != null) {
               cursor.close();
            }
         }
      }
      

      【讨论】:

        【解决方案3】:

        删除cursor.close() 声明

        【讨论】:

          【解决方案4】:

          我知道这是旧的,但我的问题最终是在 onCreate() 中,我在执行 db.execSQL(TABLE_CREATE) 后调用了 db.close()

          onCreatedbHelper.getReadableDatabase() 之后自动调用。

          这导致它崩溃,因为它使用已经关闭的对象从数据库中检索所有值。一旦我从onCreate 中删除了db.close(),一切都奏效了。

          【讨论】:

            【解决方案5】:

            对我来说,问题是我在 finally 块中关闭数据库实例以进行更新查询。

            对于无法使用上述解决方案进行修复的人,请尝试检查您的更新查询。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-09-17
              • 2012-06-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-07-28
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多