在Firebase上加载数据列表时出现java.util.ConcurrentModificationException

时间:2018-10-06 09:15:33

标签: java android firebase firebase-realtime-database

我正在尝试从Firebase加载数据,这是我加载数据的方式

    mUser = new ArrayList<>();
    reference1 = FirebaseDatabase.getInstance().getReference("USERS");
    reference1.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            mUser.clear();
            for (DataSnapshot snapshot :dataSnapshot.getChildren()){
                User user = snapshot.getValue(User.class);
                for (ChatList chatList : userList){
                    if (user.getId().equals(chatList.getId())){
                        mUser.add(user);
                    }
                }
            }

            adapter = new ChatAdapter(getContext(),mUser);
            recyclerViewchat.setAdapter(adapter);
        }

但是当我尝试运行它时遇到了这个错误,我得到了ConcurrentModificationException,即使代码在同一线程中运行,没有人能解释它为什么会发生吗?而我该如何避免呢?

10-05 16:24:14.521 24692-24692/com.example.sy.infosys_ips E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.sy.infosys_ips, PID: 24692
    java.util.ConcurrentModificationException
        at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
        at com.example.sy.infosys_ips.Chat.ChatFragment$2.onDataChange(ChatFragment.java:102)
        at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

0 个答案:

没有答案