ListActivity的IllegalStateException

时间:2013-01-29 01:10:30

标签: android sqlite listactivity listadapter illegalstateexception

我正在编写ListActivity,并且我一直在尝试重新打开一个已关闭的对象时遇到IllegalStateException。 (错误指向我的setListAdapter()方法。)任何想法?我已尝试使用startManagingCursor()以及在onPause()中调用cursor.close(),但没有任何东西可以消除错误。

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

1 个答案:

答案 0 :(得分:1)

attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM 'news';)

似乎你在这里返回一个已经关闭的光标:

cursor = dba.read("news", null, null);

检查你是否在dbAdapter read()方法中关闭了游标 如果您发布dbAdapter类,它将会有所帮助。