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进行崩溃:尝试重新打开一个已经关闭的对象:android.database.sqlite.sqliteQuery(msql = SELECT * FROM登录).请帮我解决错误
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
解决方法
你是否试图移动:
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; }