1

我收到的例外,同時具有導航向前和向後從FragmentActivity操作,含有來自定義加載程序(它是從本地SQLite數據庫加載),不知道如何處理它加載數據的ListView ...如何在執行swapCursor()時處理ConcurrentModificationException?

這裏是一個堆棧跟蹤:

Uncaught exception: Unable to destroy activity {com.snyer.bestprice/com.snyer.bestprice.PriceTracerActivity}: java.lang.RuntimeException: Unable to destroy activity {com.snyer.bestprice/com.snyer.bestprice.CartFragmentActivity}: java.util.ConcurrentModificationException 
java.lang.RuntimeException: Unable to destroy activity {com.snyer.bestprice/com.snyer.bestprice.PriceTracerActivity}: java.lang.RuntimeException: Unable to destroy activity {com.snyer.bestprice/com.snyer.bestprice.CartFragmentActivity}: java.util.ConcurrentModificationException 
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3106) 
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3171) 
    at android.app.ActivityThread.access$2100(ActivityThread.java:132) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1071) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:150) 
    at android.app.ActivityThread.main(ActivityThread.java:4293) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.RuntimeException: Unable to destroy activity {com.snyer.bestprice/com.snyer.bestprice.CartFragmentActivity}: java.util.ConcurrentModificationException 
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3106) 
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2994) 
    at android.app.LocalActivityManager.dispatchDestroy(LocalActivityManager.java:625) 
    at android.app.ActivityGroup.onDestroy(ActivityGroup.java:85) 
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3088) 
    ... 11 more 
Caused by: java.util.ConcurrentModificationException 
    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576) 
    at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:42) 
    at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:54) 
    at android.support.v4.widget.CursorAdapter.swapCursor(CursorAdapter.java:352) 
    at com.snyer.bestprice.CartFragmentActivity.onLoaderReset(CartFragmentActivity.java:412) 
    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.destroy(LoaderManager.java:337) 
    at android.support.v4.app.LoaderManagerImpl.doDestroy(LoaderManager.java:773) 
    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:318) 
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3088) 
    ... 15 more 

例外occures在這個代碼片段(CartFragmentActivity.java:412)的mCartCursorAdapter.swapCursor(null);行:

public void onLoaderReset(Loader<Cursor> loader) { 
    switch (loader.getId()) { 
    case LOADER_CART_LIST_ID: 
     mCartListCursorAdapter.swapCursor(null); 
     break; 

    case LOADER_CART_CONTENTS_ID: 
     mCartCursorAdapter.swapCursor(null); 
     break; 
    } 
} 

什麼可以做在這裏刪除這個異常?

+0

您是否找到工作解決方案或原因?我得到了完全相同的問題。你讀過:http://stackoverflow.com/questions/8189466/java-util-concurrentmodificationexception? – phlebas

+0

@phlebas在最近的將來,我已經離開了這個問題一段時間了......正在忙於項目中的其他一些錯誤/行爲,但這是我肯定會回來的問題。 – Prizoff

+0

從我可以告訴這隻發生在運行2.3.x的設備上。你能證實這一觀察嗎? – phlebas

回答

4

的SDK源給出了一個很好的提示爲註釋:

在安卓4.0.4,它是:

public void notifyInvalidated() { 
    synchronized(mObservers) { 
     for (int i = mObservers.size() - 1; i >= 0; i--) { 
      mObservers.get(i).onInvalidated(); 
     } 
    } 
} 

在早期版本是:

synchronized (mObservers) { 
     for (DataSetObserver observer : mObservers) { 
      observer.onInvalidated(); 
     } 
    } 

4.0。 4源對onChange方法中的相同更改有以下評論:

// since onChanged() is implemented by the app, it could do anything, including 
// removing itself from {@link mObservers} - and that could cause problems if 
// an iterator is used on the ArrayList {@link mObservers}. 
// to avoid such problems, just march thru the list in the reverse order. 

我不知道以任何非法的方式操縱觀察者的ArrayList。由於這在以後的API中得到了解決,我無法想象會對我的應用程序產生任何影響,我認爲我只是簡單地忽略帶有日誌條目或類似事件的異常。

0

這可能是由Loader在後臺線程上調用onLoaderReset()。嘗試使用runOnUiThread()或類似的機制將您的swapCursor()調用移動到主應用程序線程。

+0

它沒有幫助。我收到相同的ContentModificationException。 – Prizoff

0

DataSetObservable從「正確」的sdk版本複製到您的應用並使用它的最快方法。 DataSetObservable應該是這樣的:

public class DataSetObservable extends Observable<DataSetObserver> { 
/** 
* Invokes {@link DataSetObserver#onChanged} on each observer. 
* Called when the contents of the data set have changed. The recipient 
* will obtain the new contents the next time it queries the data set. 
*/ 
public void notifyChanged() { 
    synchronized(mObservers) { 
     // since onChanged() is implemented by the app, it could do anything, including 
     // removing itself from {@link mObservers} - and that could cause problems if 
     // an iterator is used on the ArrayList {@link mObservers}. 
     // to avoid such problems, just march thru the list in the reverse order. 
     for (int i = mObservers.size() - 1; i >= 0; i--) { 
      mObservers.get(i).onChanged(); 
     } 
    } 
} 

/** 
* Invokes {@link DataSetObserver#onInvalidated} on each observer. 
* Called when the data set is no longer valid and cannot be queried again, 
* such as when the data set has been closed. 
*/ 
public void notifyInvalidated() { 
    synchronized (mObservers) { 
     for (int i = mObservers.size() - 1; i >= 0; i--) { 
      mObservers.get(i).onInvalidated(); 
     } 
    } 
} 

在你的情況,你應該從源代碼複製BaseAdapter了。並將其更改爲android DataSetObservable到您的應用程序DataSetObservable

相關問題