简体   繁体   中英

Concurrent Modification Exception in Array list of users

I was done this code to the show each user, who chat with the logged in user. this code gives me an error of ConcurrentModificationException.this happens when I reply to the message.

I used Array lists for store data that are retrieved from the firebase. the error causes in the code line of " for (User user1 : users) { ". How to fix this issue.

 private void readChats() {

        users = new ArrayList<>();

        reference = FirebaseDatabase.getInstance().getReference("Users");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                users.clear();

                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    User user = snapshot.getValue(User.class);

                    // Display each user from the chats
                    for (String id : usersList) {
                        if (user.getId().equals(id)) {
                            if (users.size() != 0) {
                                for (User user1 : users) { // getting error
                                    if (!user.getId().equals(user1.getId())){
                                        users.add(user);
                                    }
                                }
                            } else {
                                users.add(user);
                            }
                        }
                    }
                }

                usersAdapter= new UsersAdapter(getContext(), users);
                recyclerView.setAdapter(usersAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


    }

This giving following error messages.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.dasun96.vreyedoctor, PID: 20759
    java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.next(ArrayList.java:860)
        at com.dasun96.vreyedoctor.ChatsFragment$2.onDataChange(ChatsFragment.java:93)
        at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
        at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
        at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)

Can you please try users = new CopyOnWriteArrayList<>(); instead of users = new ArrayList<>(); ?

ArrayLists are fail-fast, meaning they will throw out an exception when you try to do an illegal operation. An example of an illegal operation may be adding/removing from an ArrayList while iteration through it (without an iterator).

Long story short, use a CopyOnWriteArrayList and you won't have any more headaches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM