0

更改方向时出现 IllegalStateException。我有一个使用SimpleCursorAdapter和 ContentProvider 的 listView。有什么想法会导致此异常吗?

编辑

我必须更改某些内容,因为我只在从操作栏微调器中选择项目然后更改方向时才看到异常。操作栏微调器具有三个项目:显示全部、显示日期、显示位置

当用户选择一个时,它会查询数据库(参见 onNavigationItemSelected())。我尝试在 onStop() 中关闭光标,但这并没有解决问题。哪里是关闭它的正确位置?

在 MainActivity 中:

 private  Cursor mCursor = null;

 public void onCreate() {
    mSimpleCursorAdapter = new SpecialAdapter(this, 
            R.layout.row,
            null,
            //cursor,
            PROJECTION,
            new int[] { R.id.titleID, R.id.dateTimeOrLocationID1 , R.id.dateTimeOrLocationID2 },
            CursorAdapter.NO_SELECTION);

    mListView = (ListView) findViewById(android.R.id.list);
    mListView.setAdapter(mSimpleCursorAdapter);


    mOnNavigationListener = new OnNavigationListener() {

          @Override
          public boolean onNavigationItemSelected(int position, long itemId) {

              switch(position) { 
              case 0:
                  mCursor = getContentResolver().query(ReminderContentProvider.CONTENT_URI, PROJECTION, null, null, null);
                  break;
              case 1:

                  mCursor = getContentResolver().query(ReminderContentProvider.CONTENT_URI, PROJECTION, " Date NOT NULL", null, null);
                  break;
              case 2:
                  mCursor = getContentResolver().query(ReminderContentProvider.CONTENT_URI, PROJECTION, " Address NOT NULL", null, null);
                  break;
              default:
                  break;
              }
            getLoaderManager().restartLoader(0, null, MainActivity.this);
            return true;
          }
        };
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    CursorLoader loader = new CursorLoader(this, ReminderContentProvider.CONTENT_URI, PROJECTION, null, null, null);
    return loader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mSimpleCursorAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mSimpleCursorAdapter.swapCursor(null);
}

简单光标适配器:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Cursor mCursor = (Cursor) getItem(position); // exception
    if(mCursor != null)
    {
              ........
    }
 }

编辑

E/ACRA    ( 3348): com.example.locationreminder fatal error : attempt to re-open an already-closed   object: SQLiteQuery: SELECT _id, Title, Date, Address, Radius, Repetition FROM reminder
E/ACRA    ( 3348): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT _id, Title, Date, Address, Radius, Repetition FROM reminder
E/ACRA    ( 3348):  at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
E/ACRA    ( 3348):  at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
E/ACRA    ( 3348):  at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:151)
E/ACRA    ( 3348):  at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:124)
E/ACRA    ( 3348):  at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:213)
E/ACRA    ( 3348):  at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:162)
E/ACRA    ( 3348):  at android.widget.CursorAdapter.getItem(CursorAdapter.java:207)
E/ACRA    ( 3348):  at com.example.locationreminder.SpecialAdapter.getView(SpecialAdapter.java:51)
E/ACRA    ( 3348):  at android.widget.AbsListView.obtainView(AbsListView.java:2271)
E/ACRA    ( 3348):  at android.widget.ListView.makeAndAddView(ListView.java:1769)
4

1 回答 1

1

我不确定这是失败的原因,但您的代码中显然存在错误:您尝试将手动光标管理与加载程序混合使用。在OnNavigationItemSelected你打电话changeCursor。有几个问题:

  1. 您在 UI 线程中加载数据,加载程序的存在是为了避免这种情况
  2. changeCursor关闭旧光标。但是这个游标是loader所有的,你不应该关闭它,它调用后会被loader关闭OnLoadFinished
  3. 目前尚不清楚手动创建的游标在何处关闭。

您应该做的OnNavigationItemSelected是使用新的查询参数重新启动加载程序。

于 2013-08-21T02:31:02.097 回答