java.lang.IllegalStateException: attempt to re-open an already-closed object(Tried closing )
You should call getCount()
before you close your cursor.
Move:
if(cursor != null && !cursor.isClosed()){
cursor.close();
}
below:
cursor.getCount();
like this:
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;
}
java.lang.IllegalStateException: attempt to re-open an already-closed
Your error is thrown because you call cursor.getCount()
on Cursor
that you already close and this is not allowed.
So either try to use try-finally block where in finally block you close your Cursor
or assign cursor.getCount()
to int value and close Cursor
immediately.
But i recommend to you use first approach:
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();
}
}
}
remove the cursor.close()
statement