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

Sto cercando di ottenere il totalnumber di record del database, ma il registrata è caduto ogni volta con java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM login). Vi prego di aiutare con l'errorejava.lang.IllegalStateException: tentativo di riaprire un oggetto già chiuso (chiusura provato)

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 

risposta

22

Hai provato a muoversi:

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

di seguito:

cursor.getCount(); 

come questo:

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

voglio dire che si dovrebbe chiamare getCount() prima di chiudere il cursore. –

5

java.lang .IllegalS tateException: tentativo di riaprire un già-chiuso

tuo errore viene generato perché si chiama cursor.getCount() su Cursor che già vicino e questo non è consentito.

Quindi, o tenta di utilizzare try-finally blocco in cui nel blocco finally si chiude la Cursor o assegnare cursor.getCount() a int immediatamente il valore e vicino Cursor.

ma vi consiglio di utilizzare primo approccio:

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

So che questo è vecchio, ma il mio problema è che finito in onCreate(), mi stava chiamando db.close() dopo l'esecuzione db.execSQL(TABLE_CREATE).

onCreate viene chiamato automaticamente dopo dbHelper.getReadableDatabase().

Ciò ha provocato l'arresto anomalo del sistema perché è andato a recuperare tutti i valori dal database con un oggetto già chiuso. Una volta rimosso lo db.close() da onCreate, tutto funzionava.

1

rimuovere il cursor.close() dichiarazione

+0

Questo non fornisce una risposta alla domanda. Una volta che hai [reputazione] sufficiente (http://stackoverflow.com/help/whats-reputation) sarai in grado di [commentare qualsiasi post] (http://stackoverflow.com/help/privileges/comment); invece [fornisci risposte che non richiedono chiarimenti da parte del richiedente] (http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- i-do-, invece). - [Dalla recensione] (/ recensione/post di bassa qualità/12997199) – StepUp

+1

grazie AndroidMechanic, lo farà –

+0

Questo ha risolto il mio problema – JasonStack

Problemi correlati