我从FragmentActivity向前和向后导航操作时收到异常,其中包含从自定义Loader加载的数据的ListView(它从本地sqlite数据库加载),并且不知道如何处理它...
这是一个堆栈跟踪:
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
此代码片段的行mCartCursorAdapter.swapCursor(null);
发生异常(CartFragmentActivity.java:412):
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 :(得分:4)
sdk源提供了一个很好的提示作为评论:
在Android 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中解决的,我想不出对我的应用程序有任何影响我想我只是想用日志条目或类似的东西忽略异常。
答案 1 :(得分:0)
可能是onLoaderReset()
在后台线程上调用Loader
。尝试使用runOnUiThread()
或类似机制将swapCursor()
调用移动到主应用程序线程。
答案 2 :(得分: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
。