2013-03-11 99 views
11
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(); 
    } 

我試圖讓在數據庫中記錄的TOTALNUMBER,但數據庫是越來越墜毀每次與java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM login)。請幫我錯誤你java.lang.IllegalStateException:嘗試重新打開一個已關閉的對象(試過關)

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 

回答

22

試圖移動:

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; 
} 
+0

我的意思是你應該在關閉遊標之前調用getCount()。 – 2013-03-11 12:28:06

5

的java.lang .IllegalS tateException:嘗試重新打開一個已關閉

你的錯誤被拋出,因爲你打電話Cursorcursor.getCount()你已經關閉,這是不允許的。

因此,無論嘗試試,終於塊,其中使用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(); 
     } 
    } 
} 
0

我知道這是老了,但我的問題結束了,在onCreate(),我正在執行db.execSQL(TABLE_CREATE)後調用db.close()

onCreate自動調用後dbHelper.getReadableDatabase()

這導致它崩潰,因爲它然後用一個已經關閉的對象檢索數據庫中的所有值。一旦我從onCreate中刪除db.close(),它一切正常。

1

刪除cursor.close()聲明

+0

這不提供問題的答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/12997199) – StepUp 2016-07-14 06:53:30

+1

謝謝AndroidMechanic,會做 – 2016-08-02 13:55:07

+0

這解決了我的問題 – JasonStack 2017-06-10 12:54:31

相關問題