提问人:Dash 提问时间:6/7/2023 更新时间:6/7/2023 访问量:40
为新的 ArrayList 初始化抛出 CME
CME Thrown for new ArrayList Initialization
问:
我遇到了以下问题:ConcurrentModificationException
@MainThread
private void notifyAdditionalItemsLoaded(
final @Nullable List<RandomItem> newAdditionalItems) {
if (newAdditionalItems != null) {
eventListener.onAdditionalItemsLoaded(
new ArrayList<>(newAdditionalItems));
}
}
异常的堆栈跟踪为:
java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.size(ArrayList.java:1057)
at java.util.AbstractCollection.toArray(AbstractCollection.java:139)
at java.util.ArrayList.<init>(ArrayList.java:191)
at com.dash.module.component.model.datasource.notifyAdditionalItemsLoaded(datasource.java:434)
at com.dash.module.component.model.datasource.access$2300(datasource.java:42)
at com.dash.module.component.model.datasource$LoadAdditionalRecordsTask.onPostExecute(datasource.java:599)
at com.dash.module.component.model.datasource$LoadAdditionalRecordsTask.onPostExecute(datasource.java:540)
at com.dash.util.BreadcrumbTask.onPostExecute(BreadcrumbTask.java:43)
at android.os.AsyncTask.finish(AsyncTask.java:771)
at android.os.AsyncTask.-$$Nest$mfinish(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8757)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
从堆栈跟踪中可以看出,是从异步任务的方法调用的,该方法只是将处理任务设置为并调用该方法:notifyAdditionalItemsLoaded()
onPostExecute()
null
notify..
@Override
protected void onPostExecute(@Nullable List<RandomItem> items) {
loadAdditionalItemsTask = null; // reset
notifyAdditionalItemsLoaded(items);
}
列表在任务中初始化,其中新对象被初始化并添加到列表中。items
RandomItem
但是,我不确定为什么 CME 会因将对象浅层复制到列表中而被抛出。而且,这并不是一致的复制。
这里的任何帮助将不胜感激。泰亚!
答: 暂无答案
评论
newAdditionalItems
ArrayList