Android的SQLite的SQLiteOpenHelper IllegalStateException异常 - 数据库已经关闭错误 [英] Android SQLite SQLiteOpenHelper IllegalStateException - DB Already Closed Error

查看:446
本文介绍了Android的SQLite的SQLiteOpenHelper IllegalStateException异常 - 数据库已经关闭错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经快把我逼疯了,现在几天。我有一个Android应用程序是pretty的复杂。它使用多线程来从服务器获取数据并填充SQLite数据库。我使用的是单引用我SQLiteOpenHelper的扩展。我打开和关闭数据库中的每个的我的活动

This has been driving me crazy for a few days now. I have an android application that is pretty complex. It uses multiple threads to pull data from a server and populate the SQLite database. I'm using a singleton to reference my extension of SQLiteOpenHelper. I'm opening and closing the database in each of my activities.

该错误只发生在一个情况下,当我4的活动深,然后试着背出来。我试着打开和关闭数据库,包括移动接近从的onDestroy()到的onPause()方法,并且还加入另一个开到onResume()。

The error ONLY occurs in a situation when I'm 4 activities deep and then try to back out. I've tried various ways of opening and closing the database, including moving the close from the onDestroy() to the onPause() methods and also adding another open to the onResume().

另外值得注意的,我的活动大量使用列表视图和ExpandableListViews,我的理解都可能导致数据库的基础上本文关闭:<一href="http://darutk-oboegaki.blogspot.com/2011/03/sqlitedatabase-is-closed-automatically.html">http://darutk-oboegaki.blogspot.com/2011/03/sqlitedatabase-is-closed-automatically.html

Also of note, my activities make heavy use of ListViews and ExpandableListViews, which I understand could cause the database to close based on this article: http://darutk-oboegaki.blogspot.com/2011/03/sqlitedatabase-is-closed-automatically.html

我已经通过了code消失,并确保我现在不是关闭所有的游标,或者,如果他们被分配到一个适配器,调用startManagingCursor()。

I've gone through the code and made sure i'm either closing all my cursors, or, if they are being assigned to an adapter, calling startManagingCursor().

任何人是否有一个线索,这是怎么回事?

Does anybody have a clue as to what's going on?

java.lang.RuntimeException: Unable to resume activity {com.fieldone/com.fieldone.DispatchActivity}: java.lang.IllegalStateException: database /data/data/com.fieldone/databases/InterstateAirConditioning-1602814322.db already closed
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3347)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3362)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2162)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:144)
    at android.app.ActivityThread.main(ActivityThread.java:4937)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: database /data/data/com.fieldone/databases/InterstateAirConditioning-1602814322.db already closed
    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:237)
    at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:145)
    at android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:567)
    at android.app.Activity.performRestart(Activity.java:3836)
    at android.app.Activity.performResume(Activity.java:3857)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3337)
    ... 10 more

更新:我已经解决了这一问题,但不知道为什么这固定它。所以,也许有人在那里知道或能解释。

UPDATE: I've fixed the problem, but not sure WHY this fixed it. So, maybe someone out there knows or could explain.

当我在活动堆栈的第4个活动,我试图关闭数据库,通过db.close()。无论在哪里我把这个,在OnCreate后,我得到我需要的数据,或者在的onStop或的onDestroy,就会产生这个错误。如果我不关闭数据库,我不会有问题。那么,什么原因造成的DB自动关闭。奇怪的是,虽然我使用的是expandableListView在这最后的活动中,我没有使用一个CursorAdapter的。任何人有什么想法?很想了解这一点。

When I'm in the 4th activity of the stack of activities, I was attempting to close the db via db.close(). No matter WHERE I put this, in the onCreate after I get the data i need, or in the onStop or onDestroy, it will produce this error. If I DO NOT close the db, I'm not having the problem. So, something is causing the db to automatically close. The strange thing is, although I'm using an expandableListView in this final activity, I'm NOT using a cursorAdapter. Anyone have any thoughts? Would love to understand this.

推荐答案

既然这个问题已经得到了大量的关注,我想回答这个问题,并说明我学到的东西,并为我工作来纠正这个问题,我的大,数据库驱动的应用程序:

Since this question has gotten a good deal of attention, I wanted to answer it and describe what I've learned and what worked for me to correct this problem in my large, database-driven application:

  1. 不要使用管理游标。还有一个原因,为什么他们去precated。他们是完全有问题。实际上,有极少数的情况下,您确实需要使用托管光标反正。相反,运行查询,并填充对象的结果。如果要查询的多行,创建一个ArrayList&lt;您的对象>来保存所有行。我现在要做的就是创建一个运行查询,并通过我回到我的ArrayList℃的功能;>而不是光标的回报。我关闭游标函数内,我就完了。对于一个列表视图,您将不再能够使用SimpleCursorAdapter。只要把所有这些为BaseAdapter和使用您的ArrayList&LT;>对象来填充它

  1. Do NOT use managed cursors. There's a reason why they're deprecated. They're completely problematic. Realistically, there's very few scenarios where you actually need to use a managed cursor anyway. Instead, run your query and populate an object with the results. If you're querying for multiple row, create an ArrayList<> of your object to hold all the rows. What I do now is create a function that runs the query and passes me back my ArrayList<> rather than a cursor in the return. I close the cursor within the function and I'm done. For a ListViews, you'll no longer be able to use a SimpleCursorAdapter. Just convert all of these to a BaseAdapter and use your ArrayList<> object to populate it.

不要忘记关闭所有游标。这也可以严重破坏你的应用程序的数据库连接。我想我这样做,但,果然,发现我并没有显式关闭光标的位置。因此,通过你的应用程序,并仔细检查所有的人。

DO NOT FORGET TO CLOSE ALL YOUR CURSORS. This can also wreak havoc on your app's db connections. I thought I was doing this but, sure enough, found a spot that I wasn't explicitly closing a cursor. So, go through your app and double check all of them.

我还使用一个单独DatabaseHelper对象。我在我的SQLiteOpenHelper类中声明静态DatabaseHelper对象,这样我每次都得到相同的实例。

I'm also using a singleton DatabaseHelper object. I declare a static DatabaseHelper object within my SQLiteOpenHelper class so that I'm getting the same instance every time.

我现在有一个稳定运行的应用程序,不再得到这些DB错误。我希望这些信息是有帮助的一些你。

I now have a stable running app that no longer gets these DB errors. I hope this information is helpful to some of you.

这篇关于Android的SQLite的SQLiteOpenHelper IllegalStateException异常 - 数据库已经关闭错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆