android Caused by: java.util.ConcurrentModificationException

First look at the logcat log:

2021-06-25 10:47:47.262 29869-29869/com.***.*** E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.***.***, PID: 29869
    java.lang.RuntimeException: Error receiving broadcast Intent { act=com.***.servicecallback.content flg=0x10 (has extras) } in com.***.***[email protected]
        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52497(LoadedApk.java:1323)
        at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.$m$7(Unknown Source:4)
        at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.run(Unknown Source:39)
        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:6494)
        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:807)
     Caused by: java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.next(ArrayList.java:860)
        at com.***.***.MainActivity$SocketMessageReceiver.onReceive(MainActivity.java:733)
        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52497(LoadedApk.java:1313)
        at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.$m$7(Unknown Source:4) 
        at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.run(Unknown Source:39) 
        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:6494) 
        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:807) 

At first, I thought it was the problem of broadcast onreceiver

java.lang.RuntimeException: Error receiving broadcast Intent { act=com.***.servicecallback.content flg=0x10 (has extras) } in com.***.***[email protected]

Later, I found out that it was caused by the following sentence:

 Caused by: java.util.ConcurrentModificationException

Look at the reason again: if you modify vector and ArrayList at the same time during iteration, you will throw a Java. Util. Concurrent modificationexception.

The reason is that I use the list when I process data in onreceiver. When I traverse and iterate the query, I insert the data, which leads to the collapse of the list.


Read More: