IllegalStateException:数据库已关闭(使用ViewPager)

时间:2012-10-03 05:13:28

标签: android android-viewpager android-cursoradapter android-sqlite android-cursor

我对导致此错误的原因感到困惑,因为我已确保正在关闭我的数据库适配器(至少我认为我是)。这就是LogCat所说的(所有这些标签都是AndroidRuntime):

  

致命的例外:主要       java.lang.IllegalStateException:database /data/data/com.acedit.assignamo/databases/data.db(conn#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)       at java.lang.reflect.Method.invokeNative(Native Method)       在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(本地方法)

我的代码:

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.");
}

Cursor返回的fetchAllAssignments()用于通过自定义ListView填充CursorAdapterListViewListFragment的内容,并且ViewPager的每个页面中都显示了此ListFragment的多个实例(一个用于显示所有课程的分配,然后是每个个人的页面)课程)。

关于什么是错的任何想法?据我所知,我正确地关闭了我的数据库。如果您需要更多代码,请与我们联系。提前谢谢!

编辑:数据从数据库中拉出来,并且它也填充了ListView。我可以浏览各个页面,但只要Fragment停止,它就会崩溃。当我启动另一个Activity时,它经常会崩溃,但并非总是如此。当我浏览页面时,它也会偶尔崩溃,尽管很少。这是我的片段的onPause()方法(我没有onStop()或onDestroy()):

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

在我的onResume()中,我只是从数据库重新加载列表,并通过getActivity()获取上下文的新副本。也许某些事情已被清除(出于记忆目的)并且我没有重新加载它?我没有得到任何类型的NullPointerException,所以这似乎不太可能......

2 个答案:

答案 0 :(得分:3)

实际上会引发此异常,因为您双重关闭数据库,而不是因为您没有关闭它。

所以..

在您的close()方法中,替换代码中的以下行:

if (db != null) {

使用:

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

答案 1 :(得分:2)

this post

中的说明,将DbHelper设为单身人士