2012-09-22 32 views
0

當我點擊主頁按鈕onPause方法被調用,但當我調用應用程序在前面onResume被調用,並且它崩潰。當onResume被稱爲應用程序拋出ArrayIndexOutOfBouds

有代碼,當指數超出bouds的:

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在這裏,但如果它是不是應用程序甚至不啓動

的onResume:

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



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

Run方法:

@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); 
      } 
     } 
+0

請把整個onresume代碼... –

+1

球,ballX和ChangingY都是相同的大小? – DaveJohnston

+0

不,他們不是 –

回答

1

您是基於balls大小循環,但你的循環內您所使用的指數i,訪問這兩個ballXChangingY。如果ballX比球小,那麼你會得到一個ArrayIndexOutOfBoundsException。

因爲你是在循環過程中更新陣列ballX內的值,那麼你可能要創建使用balls大小循環之前一個新的數組開始:

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

不知道什麼ChangingY是,但你需要確保它至少和球一樣大。

+0

ballx是四個球的x位置,例如1,2,3,4 ChangingY是四個球的y位置 我在onResume中定義了這個? –

+0

您需要顯示更多的代碼來顯示ballX和ChangingY的定義和初始化位置。 – DaveJohnston

相關問題