本文介绍了IllegalStateException异常:数据库已经关闭(使用ViewPager)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我难住了,什么原因造成这个错误,因为我已经确定,我正确地关闭我的数据库适配器(至少我认为我)。下面是LogCat中的说法(适用于所有的标签是AndroidRuntime):

I'm stumped as to what is causing this error, as I have made sure that I am closing my database adapter properly (at least I think I am). Here's what LogCat's saying (the tag for all of them is AndroidRuntime):

致命异常:主要      java.lang.IllegalStateException:数据库/data/data/com.acedit.assignamo/databases/data.db(康涅狄格州#0)已关闭      在android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)      在android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:413)      在android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:400)      在android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)      在android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)      在android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:147)      在android.database.AbstractCursor.moveToPosition(AbstractCursor.java:178)      在android.support.v4.widget.CursorAdapter.getItemId(CursorAdapter.java:225)      在android.widget.AbsListView.onSaveInstanceState(AbsListView.java:1569)      在android.view.View.dispatchSaveInstanceState(View.java:9868)      在android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:2310)      在android.widget.AdapterView.dispatchSaveInstanceState(AdapterView.java:770)      在android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2296)      在android.view.View.saveHierarchyState(View.java:9851)      在android.support.v4.app.FragmentManagerImpl.saveFragmentViewState(FragmentManager.java:1561)      在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:962)      在android.support.v4.app.FragmentManagerImpl.detachFragment(FragmentManager.java:1233)      在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:620)      在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)      在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)      在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)      在android.support.v4.view.ViewPager.populate(ViewPager.java:895)      在android.support.v4.view.ViewPager.populate(ViewPager.java:772)      在android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1539)      在android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1422)      在android.view.View.getDisplayList(View.java:10406​​)      在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)      在android.view.View.getDisplayList(View.java:10380)      在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)      在android.view.View.getDisplayList(View.java:10380)      在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)      在android.view.View.getDisplayList(View.java:10380)      在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)      在android.view.View.getDisplayList(View.java:10380)      在android.view.HardwareRenderer $ GlRenderer.draw(HardwareRenderer.java:875)      在android.view.ViewRootImpl.draw(ViewRootImpl.java:1910)      在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)      在android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)      在android.os.Handler.dispatchMessage(Handler.java:99)      在android.os.Looper.loop(Looper.java:137)      在android.app.ActivityThread.main(ActivityThread.java:4575)      在java.lang.reflect.Method.invokeNative(本机方法)      在java.lang.reflect.Method.invoke(Method.java:511)      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)      在dalvik.system.NativeStart.main(本机方法)

我的code:

public Cursor fetchIncompleteAssignments(Short course) {
    DbAdapter adapter = new DbAdapter(context, null, Values.ASSIGNMENT_TABLE);
    adapter.open();

    Cursor r;
    if (course == null) // Fetching from all courses
        r = adapter.fetchAllWhere(Values.ASSIGNMENT_LIST_FETCH, Values.ASSIGNMENT_KEY_STATUS + "=" + 0, Values.ASSIGNMENT_KEY_DUE_DATE);
    else
        r = adapter.fetchAllWhere(Values.ASSIGNMENT_LIST_FETCH, Values.ASSIGNMENT_KEY_COURSE + "=" + course
            + " AND " + Values.ASSIGNMENT_KEY_STATUS + "=" + 0, Values.ASSIGNMENT_KEY_DUE_DATE);
    adapter.close();
    return r;
}

// Part of DbAdapter:
public DbAdapter open() throws SQLException {
    dbHelper = new DatabaseHelper(context);
    db = dbHelper.getWritableDatabase();
    return this;
}

public void close() {
    if (db != null) {
        try {
            db.close();
            dbHelper.close();
        } catch (NullPointerException e) {
            Log.e("Close", "Error: " + e + " " + e.getMessage());
        }
    } else
        Log.e("Close", "Error! db \"" + DATABASE_TABLE + "\" is null.");
}

光标 fetchAllAssignments()用于填充的ListView 通过自定义的CursorAdapter 。该的ListView 是内容的 ListFragment ,以及有此ListFragment在一个ViewPager的每一页显示的多个实例(一个用于表示从所有课程分配,并且然后为每个单独的课程的页)。

The Cursor returned from fetchAllAssignments() is used to populate a ListView through a custom CursorAdapter. The ListView is the content of a ListFragment, and there are multiple instances of this ListFragment displayed in each page of a ViewPager (one for showing assignments from all courses, and then a page for each of the individual courses).

任何想法,有什么不好?据我所知,我在正确的关闭我的数据库。让我知道如果你需要更多的code。在此先感谢!

Any ideas as to what is wrong? As far as I know, I'm closing my database correctly. Let me know if you need more code. Thanks in advance!

编辑:的数据是从数据库中拉细,并填充ListView的为好。我可以通过各种网页刷卡,但它似乎崩溃时的片段被停止。它会经常出现死机时,我开始另一个活动,但并非总是如此。它也偶尔会崩溃,当我通过网页我刷卡,但很少。这是我的onPause()方法,我片段(我没有一个的onStop()或的onDestroy()):

The data is pulled from the database fine, and it populates the ListView as well. I can swipe through the various pages, but it seems to crash whenever the Fragment is stopped. It will often crash when I start another Activity, but not always. It will also occasionally crash when I'm swiping through the pages, though very rarely. Here's my onPause() method for my Fragment (I don't have an onStop() or onDestroy()):

public void onPause() {
    super.onPause();
    if (!assignmentsCursor.isClosed()) {
        assignmentsCursor.close();
        assignmentsCursor = null;
    }
}

在我的 onResume()我只是重新加载列表从数据库中,并通过 getActivity获取上下文的新副本()。也许事情是越来越清零(用于存储目的),我不重装吗?我没有得到任何形式的NullPointerException,因此这似乎不太可能,我...

In my onResume() I just reload the list from the database and get a new copy of the context via getActivity(). Perhaps something is getting cleared (for memory purposes) and I'm not reloading it? I'm not getting any kind of NullPointerException, so this seems unlikely to me...

推荐答案

此异常实际上提高,因为你双击关闭数据库,不是因为你没有关闭它。

this exception actually is raised because you double close your database, not because you did not close it.

所以..

在你关闭()法,替换下面的行中的code:

in you close() method, replace the following line in your code:

if (db != null) {

if (db != null && db.isOpen()) {

这篇关于IllegalStateException异常:数据库已经关闭(使用ViewPager)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:27