android–IllegalStateException:尝试重新打开已经关闭的对象. SimpleCursorAdapter问题

android–IllegalStateException:尝试重新打开已经关闭的对象. SimpleCursorAdapter问题,第1张

概述我对android编程完全陌生.我可以看到这个问题之前曾多次提出过.但是,我仍然无法看到问题所在.我正在尝试将SQLite数据库中的数据连接到列表视图.在ListActivity中,我的onCreate如下所示:publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState

我对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: main09-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.access0(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

解决方法:

不要在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 onl oadFinished(Loader<Cursor> arg0, Cursor cursor)    {        adapter.swapCursor(cursor);    }    public voID onl oaderreset(Loader<Cursor> arg0)    {        adapter.swapCursor(null);    }}

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

总结

以上是内存溢出为你收集整理的android – IllegalStateException:尝试重新打开已经关闭的对象. SimpleCursorAdapter问题全部内容,希望文章能够帮你解决android – IllegalStateException:尝试重新打开已经关闭的对象. SimpleCursorAdapter问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106078.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存