【问题标题】:Error when removing from ArrayList从 ArrayList 中删除时出错
【发布时间】:2013-03-21 00:01:51
【问题描述】:

当我尝试从数组列表中删除一个与其他东西碰撞的项目时,我收到了这个错误。下面是我的 AsyncTask 和随之而来的 logcat 错误。有谁知道我哪里出错了?

这是我的异步任务:

public class handleCollisions extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... arg0) {
            Enemy tempE;
            for(int i = 0; i < enemies.size(); i++)
            {
                tempE = enemies.get(i);
                playerR = new Rect((int)x, (int)y, (int)x + player.getWidth(),(int) y + player.getHeight());
                enemyR = new Rect(tempE.x, tempE.y, tempE.x + tempE.width, tempE.y + tempE.height);
                if(Rect.intersects(playerR, enemyR))
                {
                    collision = true;
                    int exX = (int)tempE.x + (int)tempE.width - explosionBig.getWidth();
                    int exY = (int)tempE.y + (int)tempE.height + (int)tempE.height/2 - explosionBig.getHeight();
                    Explosion tempEX = new Explosion(exX, exY);
                    explosions.add(tempEX);
                    int ex2X = (int)x + (int)player.getWidth() - explosionBig.getWidth();
                    int ex2Y = (int)y + (int)player.getHeight() + (int)player.getHeight()/2 - explosionBig.getHeight();
                    Explosion temp2EX = new Explosion(ex2X, ex2Y);
                    explosions.add(temp2EX);
                    enemies.remove(i);
                    x = -200;
                    y = 300;
                    lives--;
                }
                Bullet tempB;
                for(int b = 0; b < bullets.size(); b++)
                {
                    tempB = bullets.get(b);
                    playerBulletR = new Rect(tempB.x, tempB.y, tempB.x + playerBullet.getWidth(), tempB.y + playerBullet.getHeight());
                    if(Rect.intersects(playerBulletR, enemyR) && tempE.x <= SCREEN_WIDTH)
                    {
                        int exX = (int)tempE.x + (int)tempE.width - explosionBig.getWidth();
                        int exY = (int)tempE.y + (int)tempE.height + (int)tempE.height/2 - explosionBig.getHeight();
                        Explosion tempEX = new Explosion(exX, exY);
                        explosions.add(tempEX);
                        enemies.remove(i);
                        bullets.remove(b);
                        score++;
                    }
                }
            }
            for(int i = 0; i < enemyBullets.size(); i++)
            {
                EnemyBullet tempEB = enemyBullets.get(i);
                playerR = new Rect((int)x, (int)y, (int)x + player.getWidth(),(int) y + player.getHeight());
                Rect enemyBR = new Rect((int)tempEB.x, (int)tempEB.y, (int)tempEB.x + tempEB.width, (int)tempEB.y + tempEB.height);
                if(Rect.intersects(playerR, enemyBR))
                {
                    collision = true;
                    int ex2X = (int)x + (int)player.getWidth() - explosionBig.getWidth();
                    int ex2Y = (int)y + (int)player.getHeight() + (int)player.getHeight()/2 - explosionBig.getHeight();
                    Explosion temp2EX = new Explosion(ex2X, ex2Y);
                    explosions.add(temp2EX);
                    x = -200;
                    y = 300;
                    enemyBullets.remove(i);
                    lives--;
                }
                Bullet tempB;
                for(int b = 0; b < bullets.size(); b++)
                {
                    tempB = bullets.get(b);
                    playerBulletR = new Rect(tempB.x, tempB.y, tempB.x + playerBullet.getWidth(), tempB.y + playerBullet.getHeight());
                    if(Rect.intersects(playerBulletR, enemyBR))
                    {
                        enemyBullets.remove(i);
                        bullets.remove(b);
                    }
                }
            }
            if(lives <= 0)
            {
                for(int i = 0; i < enemies.size(); i++)
                    enemies.remove(i);
                for(int i = 0; i < enemyBullets.size(); i++)
                    enemyBullets.remove(i);
                for(int i = 0; i < bullets.size(); i++)
                    bullets.remove(i);
                for(int i = 0; i < explosions.size(); i++)
                    explosions.remove(i);
                for(int i = 0; i < clouds.size(); i++)
                    clouds.remove(i);
                v.isItOk = false;
                FileOutputStream fos;
                try
                {
                    String mode = "touch";
                    fos = openFileOutput("current_score", Context.MODE_PRIVATE);
                    fos.write(mode.getBytes());
                    fos.close();
                } 
                catch (FileNotFoundException e) 
                {
                    e.printStackTrace();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
                score = 0;
                finish();
            }
            return null;
        }

Logcat

03-20 20:00:25.426: E/AndroidRuntime(4905): FATAL EXCEPTION: AsyncTask #3
03-20 20:00:25.426: E/AndroidRuntime(4905): java.lang.RuntimeException: An error occured while executing doInBackground()
03-20 20:00:25.426: E/AndroidRuntime(4905):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.lang.Thread.run(Thread.java:856)
03-20 20:00:25.426: E/AndroidRuntime(4905): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.ArrayList.remove(ArrayList.java:399)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at com.jister13.plane.Main$handleCollisions.doInBackground(Main.java:462)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at com.jister13.plane.Main$handleCollisions.doInBackground(Main.java:1)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-20 20:00:25.426: E/AndroidRuntime(4905):     ... 5 more

【问题讨论】:

  • 如 logcat 所示:Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1, your array size is 1 and you try to access index 1. Post your code.
  • size 为 1 时 index 应该为 0,都在 stacktrace 中。

标签: android arrays arraylist runtime-error


【解决方案1】:

正如logcat所说的

     Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at java.util.ArrayList.remove(ArrayList.java:399)
03-20 20:00:25.426: E/AndroidRuntime(4905):     at com.jister13.plane.Main$handleCollisions.doInBackground(Main.java:462)

当大小只有 1 时,您尝试访问索引 1(Array 的第二项),这意味着唯一可用的索引是 0。它发生在 Main.java 的第 462 行。至少发布AsyncTask 以获得更多帮助,但您可以查看那里,了解它为什么试图访问已删除或不存在的内容

【讨论】:

  • 谢谢,但我应该更清楚。第 462 行是哪一行?您正在尝试访问 => 大小的索引。您可能删除了某些内容并且没有考虑到这一点
【解决方案2】:

对于所有具有remove 方法的循环,将代码更改为以下示例

for(int i = 0; i < enemies.size(); i++)  

改成

for(int i = enemies.size() - 1; i > -1; i--)  

换句话说,从数组的末尾开始,当您尝试删除元素时向后退。

【讨论】:

  • 向后删除时,剩余索引不受影响。因此,如果您从 10 开始并删除索引 8,则剩余的索引 7,6... 不会受到影响,即使数组大小现在更改为小一,您仍将处理它们。
  • 谢谢,我将在以后的所有编程中继续这样做。
猜你喜欢
  • 2013-03-29
  • 2014-06-03
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多