I'm writing a ListActivity, and I keep getting an IllegalStateException about trying to reopen a closed object. (The error points to my setListAdapter() method.) Any ideas? I've tried using startManagingCursor() as well as calling cursor.close() in onPause(), but nothing get rid of the error.
NewsActivity.class
package com.lakesidebaptist.lakesidelife.ui.Content;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.View;
import android.widget.ListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.lakesidebaptist.lakesidelife.MainActivity;
import com.lakesidebaptist.lakesidelife.R;
import com.lakesidebaptist.lakesidelife.update.database.dbAdapter;
/**
* The activity that shows all the news items.
*
* @author andrew
*/
public class NewsActivity extends SherlockListActivity {
private Cursor cursor = null;
private SimpleCursorAdapter adapter = null;
dbAdapter dba = null;
// Arrays
private static String[] FROM = {"title", "body"};
private static int[] TO = {R.id.news_title, R.id.news_body};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
// Makes home button visible
actionBar.setHomeButtonEnabled(true);
// Allows home button to be used to navigate up
actionBar.setDisplayHomeAsUpEnabled(true);
}
/*
* This method is called when the user clicks the home button to go back. It
* stops this activity and returns the user to the home screen.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
onStop();
return true;
}
@Override
protected void onPause() {
super.onPause();
//dba.close();
}
@Override
protected void onResume() {
super.onResume();
//Setting up the database adapter
dba = new dbAdapter(this);
dba.open();
// Getting the Cursor of the requested data
cursor = dba.read("news", null, null);
//startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(this, R.layout.news_list, cursor, FROM, TO, 0);
setListAdapter(adapter);
setTitle(R.string.news);
}
}
Logcat
01-27 21:41:42.330: E/AndroidRuntime(986): FATAL EXCEPTION: main
01-27 21:41:42.330: E/AndroidRuntime(986): java.lang.RuntimeException: Unable to resume activity {com.lakesidebaptist.lakesidelife/com.lakesidebaptist.lakesidelife.ui.Content.NewsActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM 'news';)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.os.Looper.loop(Looper.java:123)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-27 21:41:42.330: E/AndroidRuntime(986): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 21:41:42.330: E/AndroidRuntime(986): at java.lang.reflect.Method.invoke(Method.java:521)
01-27 21:41:42.330: E/AndroidRuntime(986): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-27 21:41:42.330: E/AndroidRuntime(986): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-27 21:41:42.330: E/AndroidRuntime(986): at dalvik.system.NativeStart.main(Native Method)
01-27 21:41:42.330: E/AndroidRuntime(986): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM 'news';)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:202)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.widget.ListView.setAdapter(ListView.java:436)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.ListActivity.setListAdapter(ListActivity.java:267)
01-27 21:41:42.330: E/AndroidRuntime(986): at com.lakesidebaptist.lakesidelife.ui.Content.NewsActivity.onResume(NewsActivity.java:77)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.Activity.performResume(Activity.java:3823)
01-27 21:41:42.330: E/AndroidRuntime(986): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
01-27 21:41:42.330: E/AndroidRuntime(986): ... 12 more
attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM 'news';)
Seems you are returning an already closed cursor here:
cursor = dba.read("news", null, null);
Check if you haven't closed the cursor inside dbAdapter read() method.
And if you post the dbAdapter class it would be helpful.