2013-08-17 43 views
0

改變方向時,我得到一個IllegalStateException。我有一個使用SimpleCursorAdapter和ContentProvider的listView。 任何想法是什麼導致此異常?Android IllegalStateException異常改變getView中的方向改變SimplecursorAdapter

編輯

我一定是改變的東西,因爲我只看到異常從動作條微調選擇項目時,然後改變方向。 動作欄微調器有三項:顯示全部,顯示日期,顯示位置

當用戶選擇一個時,它會查詢數據庫(請參閱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) 
+0

顯示異常。 – kriomant

+0

我添加了例外日誌 – user2246120

回答

1

我不知道這是失敗的原因,但顯然是錯誤的您的代碼:您嘗試將手動光標管理與加載混合呃。在OnNavigationItemSelected中您可以撥打changeCursor。有幾個問題:

  1. 在UI線程你加載數據,裝載機爲避免這一
  2. changeCursor關閉舊光標。但是這個遊標是由加載程序擁有的,你不應該關閉它,在它調用之後它會被加載程序關閉。OnLoadFinished
  3. 手動創建的遊標關閉的位置並不清楚。

你應該在OnNavigationItemSelected中做的事情是用新的查詢參數重新啓動加載程序。

+0

謝謝。我編輯了代碼並刪除了changeCursor。現在異常消失了,但是即使添加了getLoaderManager()。restartLoader(),listview也不會再更新。我錯過了什麼? – user2246120