0

当我单击主页按钮时,会调用 onPause 方法,但是当我调用前面的应用程序时,会调用 onResume 并且它会崩溃。

索引超出范围时有代码:

while (isRunning) {
                if (!ourHolder.getSurface().isValid())
                    continue;

                Canvas canvas = ourHolder.lockCanvas();
                canvas.drawRGB(02, 02, 150);
                fingerx = (x - (Gball.getWidth() / 2));
                fingery = (y - (Gball.getHeight() * 2));
                for (int i = 0; i < balls.size(); i++) { // here...
                    ballX[i] = i * 155;
                    canvas.drawText("" + score, 40, 100, paint);
                    canvas.drawBitmap(balls.get(i), ballX[i], ChangingY[i],
                            null);

错误是:

FATAL EXCEPTION: Thread-9140
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.example.mygame.GFXSurface$GameSurface.run(GFXSurface.java:199)
at java.lang.Thread.run(Thread.java:864)

我知道这里的索引应该是 5,但如果是这样,应用程序甚至都不会启动

简历:

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    ourSurfaceView.resume();
}



public void resume() {
            isRunning = true;
            ourThread = new Thread(this);
            ourThread.start();
        }

运行方法:

@Override
        public void run() {

            for (int j = 0; j < 4; j++) {
                balls.add(j, RandomBall());
            }
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setTextSize(100);
            paint1.setStyle(Style.FILL);
            paint1.setColor(Color.RED);

            while (isRunning) {
                if (!ourHolder.getSurface().isValid())
                    continue;

                Canvas canvas = ourHolder.lockCanvas();
                canvas.drawRGB(02, 02, 150);
                fingerx = (x - (Gball.getWidth() / 2));
                fingery = (y - (Gball.getHeight() * 2));
                for (int i = 0; i < balls.size(); i++) {
                    ballX[i] = i * 155;
                    canvas.drawText("" + score, 40, 100, paint);
                    canvas.drawBitmap(balls.get(i), ballX[i], ChangingY[i],
                            null);
                    if (x != 0 && y != 0 && mode == 0) {
                        if (fingerx < 0) {
                            canvas.drawBitmap(Gball, 0, fingery, null);
                        } else if (fingery < 0) {
                            canvas.drawBitmap(Gball, fingerx, 0, null);
                        }
                        else
                            canvas.drawBitmap(Gball, fingerx, fingery, null);
                        checkForCollision(i);
                    }
                    if (ChangingY[i] <= canvas.getHeight()) {
                        ChangingY[i] += 1 * speed;
                    } else if (ChangingY[i] > canvas.getHeight()) {
                        ChangingY[i] = -72;
                        if (speed <= 11)
                            speed += 0.25;
                        removeAndAdd(this, i);

                    }
                }

                ourHolder.unlockCanvasAndPost(canvas);
            }
        }
4

1 回答 1

1

您正在根据 的大小进行循环,balls但在您的循环中,您正在访问两者ballXChangingY使用 index i。如果ballX小于球,那么您将获得 ArrayIndexOutOfBoundsException。

由于您ballX在循环期间更新数组中的值,因此您可能希望使用balls循环开始之前的大小创建一个新数组:

ballX = new int[balls.size()];

不知道 ChangeY 是什么,但你需要确保它至少和球一样大。

于 2012-09-22T11:44:43.283 回答