I looked at other questions regarding this problem and tried their solutions, but it did not help me unfortunately.
The problem is not of critical nature, meaning my program does not crash, it continues running, but anyway I would like not to have errors if possible.
I am using AsyncTask class to create some objects and load them up into an array. Database I use is a file obtained from the net, therefore I have no DBhelper class. I open the DB inside of doInBackround of AsyncTask. Inside it I have try-catch where I do all my db related work.
At first I had variables for db and cursors inside of the try statement and also I had closure of db and cursors in it at the end. I found one link saying try-catch-final should be used, so I did that, I moved variable declaration outsite of try-catch-final statement and managed closing of db and cursor variables in the final part, but still it does not help, errors are still here.
Here are some errors, I won’t C/P all of them:
01-25 17:22:45.142: E/Database(333): close() was never explicitly called on database ‘/data/data/stet.cityapp/app_databases/baza.db
01-25 17:22:45.142: E/Database(333): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-25 17:22:45.142: E/Database(333): at stetocina.cityapp.SplashScreen$LoadDB.doInBackground(SplashScreen.java:1)
01-25 17:22:45.142: E/Database(333): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-25 17:22:45.142: E/Database(333): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
Outline of the code:
protected Boolean doInBackground(String... params) {
SQLiteDatabase db = null;
Cursor poisCursor = null, tmpCursor = null;
String whereString;
try {
String DBpath = getDir("databases", 0).getAbsolutePath() + File.separator + "baza.db";
if (SQLiteDatabase.openDatabase(DBpath, null, SQLiteDatabase.OPEN_READONLY) == null) return false;
db = SQLiteDatabase.openDatabase(DBpath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
...
result = true;
} catch (Exception e) {
result = false;
return result;
} finally {
if (!poisCursor.isClosed() || poisCursor != null) {
poisCursor.deactivate();
poisCursor.close();
poisCursor = null;
}
if (!db.isOpen() || db != null) {
db.close();
db = null;
}
}
return result;
}
Could there be some problem that I am doing it in an AsyncTask? In the onPostExecute methond of AsyncTaks, if everything went well and result is true, I open a new Activity, and while debugging I noticed that the error is displayed only when new activity is displayed and not immediatelly after exit from doInBackground.
Thanks for the help!
Isn’t the database being opened twice?
inside the ‘if’ condition (if (SQLiteDatabase.openDatabase(DBpath, null, SQLiteDatabase.OPEN_READONLY) == null) return false;)
in the next line (db = SQLiteDatabase.openDatabase(DBpath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);)
and you are closing only the second one(db).