关于SimplecursorAdapter的getView中方向更改的Android IllegalStateException

时间:2013-08-17 18:46:54

标签: android simplecursoradapter illegalstateexception

更改方向时出现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);
}

SimpleCursorAdapter:

@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)

1 个答案:

答案 0 :(得分:1)

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

  1. 您在UI线程中加载数据,存在加载器以避免此
  2. changeCursor关闭旧光标。但是这个游标是由加载器拥有的,你不应该关闭它,它会在调用OnLoadFinished
  3. 后被加载器关闭。
  4. 目前尚不清楚手动创建的游标的关闭位置。
  5. OnNavigationItemSelected中应该做的是使用新的查询参数重新启动加载程序。