简体   繁体   中英

Not sure why there is concurrent modification exception

I am iterating through two lists of Orb objects

Currently as I go through both lists I add and remove Orb objects as I iterate through them.

//first I iterate through the "friendly" orb list.
for(orbIterator = friendlyOrbs.listIterator(); orbIterator.hasNext();) {
//I have only included what I (think) is the relevant code.        

Orb tempOrb = orbIterator.next(); //Set a reference to the current orb object so I can update it and use its properties.

        //Some drawing stuff I don't include.

        //The orb is not dead
        int orbResult = tempOrb.Update(); //Updates the orb object and returns its current state.

        if(orbResult == 0) {

        } else if(orbResult == 2) {
            //Died a natural death
            //Stuff to do when the orb dies naturally.
            orbIterator.remove();
        } else if(orbResult == 3) {
            //Killed by player.
            //stuff to do when player kills orb.
            orbIterator.remove();
            orbIterator.add(new Orb(...));
        }
    }

Now this is how I iterate through the "enemy" orb list:

   for(orbIterator = enemyOrbs.listIterator(); orbIterator.hasNext();) {
        Orb tempOrb = orbIterator.next();

        int orbResult = tempOrb.Update();

        if(orbResult == 0) {

        } else if (orbResult == 2) {
            //Enemy orb died a natural death.
            orbIterator.remove();

            enemyOrbs.add(...);
        } else if(orbResult == 3) {
            //Enemy orb killed by player.
            orbIterator.remove();
        } else {
            //Draw the orb.
        }
    }

I also have two timer tasks (one for spawning friendly orbs, one for spawning enemy orbs) that set a boolean flag that tells the onDraw method if it should add a new orb to the screen.

//the friendly orb timertask

public static class FriendlyOrbTimerTask extends TimerTask implements Cloneable {

    @Override
    public void run() {
        if(SystemClock.uptimeMillis() - drawCallTime < 100) {
            Log.i("ADDING NEW ORB", "ADDING NEW ORB");
            friendlyOrbToBeAdded = true;
            friendlyOrbSpawnInterval += 250;
            TIMER.schedule(this.clone(), friendlyOrbSpawnInterval);
        } else {
            pauseStartTime = SystemClock.uptimeMillis();
        }

    }

    @Override
    public FriendlyOrbTimerTask clone(){
        return new FriendlyOrbTimerTask(); //add parameters from the current contextif needed.
    }
}

Here is the enemyOrb timertask:

public static class EnemyOrbTimerTask extends TimerTask implements Cloneable {



    @Override
    public void run() {

        if(SystemClock.uptimeMillis() - drawCallTime < 50) {
            enemyOrbToBeAdded = true;
            enemyOrbSpawnInterval+= 250;
            TIMER.schedule(this.clone(), enemyOrbSpawnInterval);
        }
    }

    @Override
    public EnemyOrbTimerTask clone(){
        return new EnemyOrbTimerTask(); //add parameters from the current contextif needed.
    }
}

Here are the if statements that tell me if I should add a new orb to the screen (located in onDraw)

   //These are located right before I iterate through the lists.
   if(friendlyOrbToBeAdded) {
        friendlyOrbs.add(...);
        friendlyOrbToBeAdded = false;
    }
    if(enemyOrbToBeAdded) {
        enemyOrbs.add(...);
        enemyOrbToBeAdded = false;
    }

The problem is sometimes my app randomly crashes on the line that says:

  Orb tempOrb = orbIterator.next(); //This is in the enemyOrb iterator loop.

It gives the exception: java.util.ConcurrentModificationException

I don't know why this is happening because I am using listIterator.add() and listIterator.remove() which should allow me to avoid these exceptions.

If your copy-pasting is accurate, then I think this is the problem:

   for(orbIterator = enemyOrbs.listIterator(); orbIterator.hasNext();) {
        ...
            enemyOrbs.add(...);
        ...
   }

You have a call to add on the collection while you are iterating it.

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