1

我正在编写一个 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);
    }
}

日志猫

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
4

1 回答 1

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 类,它会很有帮助。

于 2013-01-29T01:27:24.223 回答