StaleDataException与对话框

我想显示与我的数据库中的名称与列表视图的对话框,但我不断收到一个StaleDataException 。 我知道这通常意味着我正在尝试使用数据从一个closures的游标,但光标不会closures,直到我得到所有的数据,所以我不明白为什么我得到这个

 d = new Dialog(this); d.setContentView(R.layout.dialog_layout); d.setTitle("Select Bowler"); ListView lv = (ListView)d.findViewById(R.id.dialog_list); Cursor c = getContentResolver().query( BowlersDB.CONTENT_URI, new String[] { BowlersDB.ID, BowlersDB.FIRST_NAME, BowlersDB.LAST_NAME }, null, null, BowlersDB.LAST_NAME + " COLLATE LOCALIZED ASC" ); if (c.moveToFirst() && c != null) { SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, R.layout.names_listview, c, new String[] { BowlersDB.FIRST_NAME, BowlersDB.LAST_NAME }, new int[] { R.id.bListTextView, R.id.bListTextView2 }, 0 ); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { bowlerClickedID = id; updateName(id); } }); d.show(); } c.close(); 

错误

 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:114) at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:150) at android.widget.CursorAdapter.getView(CursorAdapter.java:250) at android.widget.AbsListView.obtainView(AbsListView.java:2267) at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) at android.widget.ListView.onMeasure(ListView.java:1156) at android.view.View.measure(View.java:15172) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) at android.view.View.measure(View.java:15172) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:15172) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:15172) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) at android.view.View.measure(View.java:15172) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148) at android.view.View.measure(View.java:15172) ... 

编辑:如果我注释掉c.close()行它工作正常,但我不能让光标开放,但我该怎么办?

Solutions Collecting From Web of "StaleDataException与对话框"

您不能closures游标,直到不再需要CursorAdapter。 所以你可以在onDestroy()方法中closures它:

 @Override public void onDestroy() { super.onDestroy(); ListView lv = (ListView) d.findViewById(R.id.dialog_list); ((CursorAdapter) lv.getAdapter()).getCursor().close(); database.close(); } 

由于您正在closuresCursor而您创build的SimpleCursorAdapter可能仍然尝试从其中访问数据,所以会出现错误。

如果你想使用一个CursorLoader ,你应该这样做。

  1. 保持对适配器的引用作为实例variables
  2. 创build简单的游标适配器,并为游标引用null
  3. 让你的类实现LoaderManager.LoaderCallbacks<Cursor>
  4. 实际上在onCreateLoader创buildCursorLoader
  5. onLoadFinished() ,将游标与adapter.swapCursor(cursor)
  6. onLoaderReset() ,将游标与null游标交换为adapter.swapCursor(null)`

你也应该把你的数据放在一个ContentProvider中 – 这并不坏!

我曾经依靠startManagingCursor方法来打开和closuresCursors到系统的麻烦。 现在它已经被弃用了(它仍然有效,但我不推荐使用它),可以考虑在LoaderManager使用CursorLoader类,并把它留给系统来处理游标closures。