3

我对android编程完全陌生。我可以看到这个问题之前已经提出过很多次了。但是,我仍然无法看到问题所在。我正在尝试将 SQLite 数据库中的数据连接到列表视图。在 ListActivity 我的 onCreate 如下所示:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_init_key);
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    DBHandler db = new DBHandler(this);

    Cursor cursor = db.getKeyPointCursor(1, "Crataegus");
    // the desired columns to be bound
    String[] columns = new String[] { "question1","answer1" };
    // the XML defined views which the data will be bound to
    int[] to = new int[] { R.id.question, R.id.answer };

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.key_list_entry, cursor, columns, to, 0);

    cursor.close();
    this.setListAdapter(mAdapter);

}

它引发了以下异常,我希望有人可以帮助我。

09-10 21:52:01.505: W/dalvikvm(10976): threadid=1: thread exiting with uncaught exception (group=0x40c8e1f8)
09-10 21:52:01.510: E/AndroidRuntime(10976): FATAL EXCEPTION: main
09-10 21:52:01.510: E/AndroidRuntime(10976): java.lang.RuntimeException: Unable to start activity ComponentInfo{jem.danskflora/jem.danskflora.InitKeyActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM Crataegus WHERE _id=?) 
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.os.Looper.loop(Looper.java:137)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.ActivityThread.main(ActivityThread.java:4514)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at java.lang.reflect.Method.invokeNative(Native Method)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at java.lang.reflect.Method.invoke(Method.java:511)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at dalvik.system.NativeStart.main(Native Method)
09-10 21:52:01.510: E/AndroidRuntime(10976): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM Crataegus WHERE _id=?) 
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:33)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:82)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.widget.CursorAdapter.getCount(CursorAdapter.java:196)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.widget.ListView.setAdapter(ListView.java:467)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.ListActivity.setListAdapter(ListActivity.java:265)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at jem.danskflora.InitKeyActivity.onCreate(InitKeyActivity.java:30)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.Activity.performCreate(Activity.java:4562)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
09-10 21:52:01.510: E/AndroidRuntime(10976):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
09-10 21:52:01.510: E/AndroidRuntime(10976):    ... 11 more
4

3 回答 3

4

不要在您的onCreate方法中关闭光标。它被您的适配器引用,但尚未使用!

尝试使用 LoaderManager / CursorLoader。这是处理游标的新方法:How to transition from managedQuery to LoaderManager/CursorLoader?

这是我的一个项目中的一个示例(我没有测试此代码):

public class AdditionalActivitiesListFragment extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener, OnClickListener
{
    private SimpleCursorAdapter adapter;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        String[] columns = new String[] { "your", "db", "columns" };
        int[] to = new int[] { R.id.your, R.id.fields, R.id.toMapWith };

        getLoaderManager().initLoader(0x02, null, this);

        adapter = new SimpleCursorAdapter(activity.getApplicationContext(), R.layout.row_layout, null, columns, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        setListAdapter(adapter);
    }

    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1)
    {
        return new SimpleCursorLoader(this) {
            @Override
            public Cursor loadInBackground() {
                Cursor c = // Your cursor

                return c;
            }
        };
    }

    public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor)
    {
        adapter.swapCursor(cursor);
    }

    public void onLoaderReset(Loader<Cursor> arg0)
    {
        adapter.swapCursor(null);
    }
}

SimpleCursorLoader 的来源:https ://gist.github.com/1217628 (通过https://stackoverflow.com/a/7422343/615882

于 2012-09-10T20:31:03.337 回答
1

您的 SimpleCursorAdapter 与您的 Cursor 相关联,它为您的适配器“提供”来自 Cursor 的数据。将您的适配器想象成一个适配器,它位于您的游标之上并处理从游标到 ListView 的数据委托。因此,只有在不再使用 ListView 时才应关闭光标,fx。in onPause()(当 Activity 不再向用户显示时)。

于 2012-09-10T20:33:06.497 回答
0

我认为cursor继续使用adapter,所以你可能不应该在那里关闭它。

您可以使用Activity.startManagingCursor()which 将在Activity不再使用时为您关闭它。

于 2012-09-10T20:30:27.527 回答