带对话框的StaleDataException

时间:2012-09-29 17:14:24

标签: android staledataexception

我正在尝试使用列表视图显示一个对话框,其中包含我的数据库中的名称,但我不断获得StaleDataException。我知道这通常意味着我正在尝试使用来自闭合光标的数据,但光标不会关闭,直到我得到所有数据所以我不明白为什么我得到这个

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()行它可以正常工作,但我不能将光标打开,所以我该怎么办?

3 个答案:

答案 0 :(得分:9)

您收到错误是因为您关闭了Cursor,而您创建的SimpleCursorAdapter可能仍在尝试从中访问数据。

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

  1. 将对适配器的引用保留为实例变量
  2. 创建简单的游标适配器,并为游标
  3. 引用null
  4. 让您的班级实施LoaderManager.LoaderCallbacks<Cursor>
  5. 实际上在CursorLoader
  6. 中创建onCreateLoader
  7. onLoadFinished()中,使用adapter.swapCursor(cursor)
  8. 交换游标
  9. onLoaderReset()中,将光标与null光标交换为adapter.swapCursor(null)`
  10. 您最终还应该将数据放入ContentProvider中 - 这并不是那么糟糕!

答案 1 :(得分:4)

在不再需要CursorAdapter之前,无法关闭光标。所以你可以在onDestroy()方法中关闭它:

@Override
public void onDestroy() {
 super.onDestroy();

 ListView lv = (ListView) d.findViewById(R.id.dialog_list);
 ((CursorAdapter) lv.getAdapter()).getCursor().close();
 database.close();
}

答案 2 :(得分:0)

我过去依赖于startManagingCursor方法,无法打开和关闭Cursors系统。现在它已被弃用(它仍然有效,但我不建议使用它),请考虑将CursorLoader类与LoaderManager一起使用,并将其留给系统来处理Cursor关闭。