问题描述
有一个RecycleView视图组件,数据变更得时候进行了增加和删除操作,频繁触发 增加、删除数据得操作,低概率出现此问题,代码片段无法展示,可以语言描述一下代码片段
- recycleview使用了adapter来展示数据
- adapter初始化子view需要一个list
- list可能会调用remove、add操作,频繁调用会触发
ConcurrentModificationException
异常
下面是错误堆栈信息
2022-04-25 10:40:27.082 14788-14788/com.xx E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.xx, PID: 14788java.util.ConcurrentModificationExceptionat java.util.ArrayList$SubList.size(ArrayList.java:1057)at com.chad.library.adapter.base.BaseQuickAdapter.getItemCount(BaseQuickAdapter.java:698)at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3722)at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3219)at android.view.View.measure(View.java:22238)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)at android.view.View.measure(View.java:22238)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1167)at android.widget.LinearLayout.onMeasure(LinearLayout.java:687)at android.view.View.measure(View.java:22238)at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)at android.view.View.measure(View.java:22238)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)at android.view.View.measure(View.java:22238)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)at android.view.View.measure(View.java:22238)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)at android.view.View.measure(View.java:22238)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)at android.view.View.measure(View.java:22238)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)at com.android.internal.policy.DecorView.onMeasure(DecorView.java:725)at android.view.View.measure(View.java:22238)at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2523)at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1550)at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1438)at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6982)at android.view.Choreographer$CallbackRecord.run(Choreographer.java:916)at android.view.Choreographer.doCallbacks(Choreographer.java:728)at android.view.Choreographer.doFrame(Choreographer.java:660)at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:902)at android.os.Handler.handleCallback(Handler.java:790)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:164)at android.app.ActivityThread.main(ActivityThread.java:6548)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
解决方案:
- 在使用iterator迭代的时候使用synchronized或者Lock进行同步
- 使用并发容器CopyOnWriteArrayList代替ArrayList和Vector。