在 java.util.arraylist$itr.next 处的 java.util.concurrentmodificationexception

时间:2021-06-22 11:37:11

标签: java android loops iterator

我正在从 firebase 实时数据库获取数据,并且在迭代数据快照时,当像这样将数据插入房间数据库时,我在第 493 行的行号处出现 java.util.concurrentmodificationexception at java.util.arraylist$itr.next 错误。

Inventory.getInstance(Inventory.this.getActivity(), 1).InventoryDAO().InsertData(items);

这是我收到此错误的整个方法。

private List<InventoryEntity_TableColums> message; // Class Variable


      private synchronized void append_inventory_conversation(DataSnapshot dataSnapshot) {
        Iterator iterator = dataSnapshot.getChildren().iterator();
    
    
    
        String inventory_Msg, inventory_TO, inventory_FROM, inventory_TimeStamp, inventory_FCM_FROM, inventory_FCM_TO, inventory_Type, Random_ID, inventory_FROM_ID, inventory_Receiver_ID;
    
    
        while (iterator.hasNext()) {
            inventory_Msg = (String) ((DataSnapshot) iterator.next()).getValue();
            inventory_FROM = (String) ((DataSnapshot) iterator.next()).getValue();
            inventory_FROM_ID = (String) ((DataSnapshot) iterator.next()).getValue();
            inventory_TO = (String) ((DataSnapshot) iterator.next()).getValue();
            inventory_TimeStamp = (String) ((DataSnapshot) iterator.next()).getValue();
            inventory_Type = (String) ((DataSnapshot) iterator.next()).getValue();
            Random_ID = (String) ((DataSnapshot) iterator.next()).getValue();
            inventory_FCM_FROM = (String) ((DataSnapshot) iterator.next()).getValue();
            inventory_FCM_TO = (String) ((DataSnapshot) iterator.next()).getValue();
    
    
            // return count of RANDOM ID  in room
            int hasValue = inventoryRoomDatabase.getInstance(getActivity(), 1).inventoryDAO().CheckValueExist(Random_ID);
            Log.d(TAG, "Has inventory " + hasValue);
    
            if (hasValue == 0) {
                inventoryEntity_TableColums inventoryEntity_tableColums;
                if (inventory_Type.equals("1")) {
                    inventoryEntity_tableColums = new inventoryEntity_TableColums(Session.getUserID(),inventory_Msg,  "1");
                    message.add(inventoryEntity_tableColums);
                   
    
                } else if (inventory_Type.equals("2")) {
                    inventoryEntity_tableColums = new inventoryEntity_TableColums(Session.getUserID(),inventory_Msg, "2");
                    message.add(inventoryEntity_tableColums);
                   
    
                } else if (inventory_Type.equals("3")) {
                    inventoryEntity_tableColums = new inventoryEntity_TableColums(Session.getUserID(),inventory_Msg, "3");
                    message.add(inventoryEntity_tableColums);
                    
                } else if (inventory_Type.equals("4")) { //THIS IS FOR SEARCHED IMAGES NOT FOR GIF IMAGES
                    inventoryEntity_tableColums = new inventoryEntity_TableColums( Session.getUserID(),inventory_Msg,"4");
                    message.add(inventoryEntity_tableColums);
    
                   
                } else if (inventory_Type.equals("5")) {
                    //Adding inventory Data Into Database
                    inventoryEntity_tableColums = new inventoryEntity_TableColums( Session.getUserID(),inventory_Msg,"5");
                    message.add(inventoryEntity_tableColums);
    
                   
                } else if (inventory_Type.equals("6")) {
                    //Adding inventory Data Into Database
                    inventoryEntity_tableColums = new inventoryEntity_TableColums( Session.getUserID(),inventory_Msg,"6");
                    message.add(inventoryEntity_tableColums);
    
                  
                } else if (inventory_Type.equals("7")) {
                    //Adding inventory Data Into Database
                    inventoryEntity_tableColums = new inventoryEntity_TableColums(Session.getUserID(),inventory_Msg,"7");
                    message.add(inventoryEntity_tableColums);
                   
                } else if (inventory_Type.equals("8")) {
                    //Adding inventory Data Into Database
                    inventoryEntity_tableColums = new inventoryEntity_TableColums( Session.getUserID(),inventory_Msg,"8");
                    message.add(inventoryEntity_tableColums);
    
                 
    
                } else if (inventory_Type.equals("9")) {
                    //Adding inventory Data Into Database
                    inventoryEntity_tableColums = new inventoryEntity_TableColums(Session.getUserID(),inventory_Msg,"9");
                    message.add(inventoryEntity_tableColums);
    
                    
                } else if (inventory_Type.equals("10")) {
                    //Adding inventory Data Into Database
                    inventoryEntity_tableColums = new inventoryEntity_TableColums( Session.getUserID(),inventory_Msg,"10");
                    message.add(inventoryEntity_tableColums);
                  
                } else if (inventory_Type.equals("11")) {
                    //Adding inventory Data Into Database
                    inventoryEntity_tableColums = new inventoryEntity_TableColums( Session.getUserID(),inventory_Msg,"11");
                    message.add(inventoryEntity_tableColums);
    
                  
                } else if (inventory_Type.equals("12")) {
                    inventoryEntity_tableColums = new inventoryEntity_TableColums( Session.getUserID(),inventory_Msg,"12");
                    message.add(inventoryEntity_tableColums);
                    Log.d(TAG, "Message Type Message" + inventory_Msg);
    
                 
                } else if (inventory_Type.equals("13")) {
                    inventoryEntity_tableColums = new inventoryEntity_TableColums( Session.getUserID(),inventory_Msg,"13");
                    message.add(inventoryEntity_tableColums);
    
                    
    
                }
    
            }
    
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
//THIS LINE WHERE I AM GETTING java.util.concurrentmodificationexception at java.util.arraylist$itr.next at insertInventory
                inventoryRoomDatabase.getInstance(inventoryScreen.this.getActivity(), 1).inventoryDAO().Insertinventory(message);
            }
        }).start();
    }

Room 数据库的 InsertInventroy

@Insert(onConflict = OnConflictStrategy.IGNORE)
    void Insertinventory(List<InventoryEntity_TableColums> inv);

1 个答案:

答案 0 :(得分:0)

我假设在 insertinventory 方法中您使用迭代器迭代列表,并且您想向列表添加新元素。问题是你不能对修改后的列表使用相同的迭代器。