陈旧数据异常,关闭游标窗口

时间:2014-06-10 20:03:31

标签: android database android-viewpager android-cursor

我遇到了一个我无法解决的错误。

android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:139)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
        at android.database.CursorWrapper.getString(CursorWrapper.java:118)
        at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:135)
        at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
        at android.widget.AbsListView.obtainView(AbsListView.java:2435)
        at android.widget.ListView.makeAndAddView(ListView.java:1891)
        at android.widget.ListView.fillDown(ListView.java:792)
        at android.widget.ListView.fillFromTop(ListView.java:853)
        at android.widget.ListView.layoutChildren(ListView.java:1717)
        at android.widget.AbsListView.onLayout(AbsListView.java:2279)
        at android.view.View.layout(View.java:15047)
        at android.view.ViewGroup.layout(ViewGroup.java:4676)
        at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
        at android.view.View.layout(View.java:15047)
        at android.view.ViewGroup.layout(ViewGroup.java:4676)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
        at android.view.View.layout(View.java:15047)
        at android.view.ViewGroup.layout(ViewGroup.java:4676)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:15047)
        at android.view.ViewGroup.layout(ViewGroup.java:4676)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:15047)
        at android.view.ViewGroup.layout(ViewGroup.java:4676)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
        at android.view.View.layout(View.java:15047)
        at android.view.ViewGroup.layout(ViewGroup.java:4676)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:15047)
        at android.view.ViewGroup.layout(ViewGroup.java:4676)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2181)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1888)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1095)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6010)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:799)
        at android.view.Choreographer.doCallbacks(Choreographer.java:599)
        at android.view.Choreographer.doFrame(Choreographer.java:559)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:784)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5872)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
        at dalvik.system.NativeStart.main(Native Method)

我无法解决它的来源,因为我没有任何代码参考。我有一个viewpager,它根据数据库查询显示最多3个列表视图。当我更改从另一个活动提供viewpager的数据然后回退到viewpager所在的数据时,会出现上述问题。在onResume中,我将查询重新运行到数据库并重新运行我用来放置实际的加载器viewpager页面中的数据。这一切都很好,然后它崩溃了!我不认为它是其他活动,因为对数据库的更改实际发生并保存。有人有什么想法吗?

2 个答案:

答案 0 :(得分:0)

似乎谷歌试图给出答案。他们说你必须重新查询()你的光标以消除StaleException。但是他们也说你不应该再使用requery()(不推荐使用),但要重新打开游标......我使用了requery()并解决了问题。

答案 1 :(得分:0)

这很简单。在调用 cursor.close(); 操作后,您正在某处使用游标对象。

例如,

String query = "SELECT category FROM table_name where id=" +
                        String.valueOf(0);
Cursor cursor = mDatabase.rawQuery(query, null);

String category_name = cursor.getString(0);
cursor.close();

textView.setText(cursor.getString(0));

你也可以解决它, 在setText之后使用curser.close()。 或者在setText函数中使用category_name而不是cursor.getString(0)。