Egek92 Egek92 - 3 months ago 15
Android Question

Attempt to re-open an already closed object error Android SQLite

I'm trying to build a simle to-do list app adds user input to listview but keep getting this error

public List<Task> getAllTasks() {
List<Task> taskList = new ArrayList<Task>();

String selectQuery = "SELECT * FROM " + TABLE_TASKS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);


if (cursor.moveToFirst()) {
do {
Task task = new Task();
task.setId(cursor.getInt(0));
task.setTaskName(cursor.getString(1));
task.setStatus(cursor.getInt(2));
// Adding contact to list
taskList.add(task);
} while (cursor.moveToNext());
}

// return task list
return taskList;
}


getting the error on this line SQLiteDatabase db = this.getWritableDatabase();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
db = new TaskerDbHelper(this);
list = db.getAllTasks();
adapt = new MyAdapter(this, R.layout.list_inner_view, list);
ListView listTask = (ListView) findViewById(R.id.listView1);
listTask.setAdapter(adapt);
}


and here: list = db.getAllTasks();

Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/activfy.activfy/databases/taskerManager
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:520)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at activfy.activfy.TaskerDbHelper.getAllTasks(TaskerDbHelper.java:73)
at activfy.activfy.GoalActivity.onCreate(GoalActivity.java:32)


edit:

@Override
public void onCreate(SQLiteDatabase db) {

String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_TASKS + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TASKNAME
+ " TEXT, " + KEY_STATUS + " INTEGER)";
db.execSQL(sql);

db.close();
}




@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);
// Create tables again
onCreate(db);
}

Answer

Try this :

public List<Task> getAllTasks() {
List<Task> taskList = new ArrayList<Task>();

String selectQuery = "SELECT  * FROM " + TABLE_TASKS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
  try {

if (cursor.moveToFirst()) {
    do {
        Task task = new Task();
        task.setId(cursor.getInt(0));
        task.setTaskName(cursor.getString(1));
        task.setStatus(cursor.getInt(2));
        // Adding contact to list
        taskList.add(task);
    } while (cursor.moveToNext());
}
 finally
    {
        if (cursor != null)
        {
            cursor.close();
            db.close();
        }

// return task list
return taskList;
 }

And in your OnCreat() you should not close your DB.

Comments