Tag Archives: ArrayList

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.***.***.MainActivity$SocketMessageReceiver@b855039
        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.***.***.MainActivity$SocketMessageReceiver@b855039

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.

java.lang.UnsupportedOperationException resolvent

in the project List for operating times wrong Java. Lang. UnsupportedOperationException, later found operating List is composed of array transformation, by looking at the source code found problems, and write the test procedure is as follows.
code block:

public class ListTest {
    public static void main(String[] args) {
        String[] array = {"1","2","3","4","5"};
        List<String> list = Arrays.asList(array);
        list.add("6");
    }
}

execution result:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)
	at com.atguigu.test.ListTest.main(ListTest.java:11)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

The problem with

is the following: the add and remove methods in the List generated by the
call on arrays.aslist () are exception because ArrayList of the inner class of arrays.aslist () is returned instead of java.util.ArrayList. Arrays of inner class ArrayList and Java. Util. ArrayList are inherited AbstractList, remove, add method is the default in AbstractList throw UnsupportedOperationException and don’t make any operation. Java.util.ArrayList overrides these methods, but the ArrayList of Arrays internal class does not, so it will throw an exception. The solution:

public class ListTest {
    public static void main(String[] args) {
        String[] array = {"1","2","3","4","5"};
        List<String> list = Arrays.asList(array);
        List arrList = new ArrayList(list);
        arrList.add("6");
    }
}