2013-03-18 126 views
0

我被卡住了,運行GameView類時出現錯誤。它編譯沒有錯誤,但在Android模擬器上運行時它崩潰。我會在代碼後發佈logcat。Android IndexOutOfBound異常:位置0無效,大小爲0

代碼

public class GameView extends View { 

private Context myContext; 
private List<Card> deck = new ArrayList<Card>(); 
private int scaledCardW; 
private int scaledCardH; 
private int screenW; 
private int screenH; 
private List<Card> myHand = new ArrayList<Card>(); 
private List<Card> oppHand = new ArrayList<Card>(); 
private List<Card> discardPile = new ArrayList<Card>(); 
private float scale; 
private Paint whitePaint; 
private int oppScore; 
private int myScore; 

public GameView(Context context) { 
    super(context); 
    myContext = context; 
    scale = myContext.getResources().getDisplayMetrics().density; 

    whitePaint = new Paint(); 
    whitePaint.setAntiAlias(true); 
    whitePaint.setColor(Color.WHITE); 
    whitePaint.setStyle(Paint.Style.STROKE); 
    whitePaint.setTextAlign(Paint.Align.LEFT); 
    whitePaint.setTextSize(scale * 15); 

} 

private void initCards() { 
    for (int i = 0; i < 4; i++) { 
     for (int j = 102; j < 115; j++) { 
      int tempID = j + (i * 100); 
      Card tempCard = new Card(tempID); 
      int resourceID = getResources().getIdentifier("card" + tempID, 
        "drawable", myContext.getPackageName()); 
      Bitmap tempBitmap = BitmapFactory.decodeResource(
        myContext.getResources(), resourceID); 
      scaledCardW = (int) (screenW/8); 
      scaledCardH = (int) (scaledCardW * 1.28); 
      Bitmap scaledBitmap = Bitmap.createScaledBitmap(tempBitmap, 
        scaledCardW, scaledCardH, false); 
      tempCard.setBitmap(scaledBitmap); 

      deck.add(tempCard); 
     } 
    } 
} 

private void drawCard(List<Card> handToDraw) { 
    handToDraw.add(0, deck.get(0)); 
    deck.remove(0); 

    if (deck.isEmpty()) { 
     for (int i = discardPile.size() - 1; i > 0; i--) { 
      deck.add(discardPile.get(i)); 
      discardPile.remove(i); 
      Collections.shuffle(deck, new Random()); 

     } 
    } 
} 

private void dealCards() { 
    Collections.shuffle(deck, new Random()); 
    for (int i = 0; i < 7; i++) { 
     drawCard(myHand); 
     drawCard(oppHand); 
    } 
} 

@Override 
public void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 
    dealCards(); 

} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawText("Computer Score: " + Integer.toString(oppScore), 10, 
      whitePaint.getTextSize() + 10, whitePaint); 
    canvas.drawText("My Score: " + Integer.toString(myScore), 10, screenH 
      - whitePaint.getTextSize() - 10, whitePaint); 
} 

    } 

logcat的出口。

03-18 07:51:46.795: W/dalvikvm(4395): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
    03-18 07:51:46.795: E/AndroidRuntime(4395): Uncaught handler: thread main exiting due to uncaught exception 
    03-18 07:51:46.805: E/AndroidRuntime(4395): java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at java.util.ArrayList.get(ArrayList.java:341) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at com.agpfd.crazyeights.GameView.drawCard(GameView.java:66) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at com.agpfd.crazyeights.GameView.dealCards(GameView.java:81) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at com.agpfd.crazyeights.GameView.onSizeChanged(GameView.java:89) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.view.View.setFrame(View.java:6897) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.view.View.layout(View.java:6824) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.view.View.layout(View.java:6830) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.view.View.layout(View.java:6830) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.view.ViewRoot.performTraversals(ViewRoot.java:996) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.os.Looper.loop(Looper.java:123) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at java.lang.reflect.Method.invokeNative(Native Method) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at java.lang.reflect.Method.invoke(Method.java:521) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
    03-18 07:51:46.805: E/AndroidRuntime(4395):  at dalvik.system.NativeStart.main(Native Method) 

任何幫助將不勝感激。

+0

什麼是在'GameView.java'的第66行? – 2013-03-18 08:21:50

回答

3

initCards()是從來沒有在任何地方調用,所以甲板列表是空的,所以調用handToDraw.add(0, deck.get(0))drawCard()導致你有例外,這清楚地表明瞭問題:你想在一個列表索引0來訪問元素大小0:

Invalid location 0, size is 0 
+0

謝謝大家的幫助,我將initCards()添加到onSizeChange並添加了一些其他變量,它工作。再次感謝。 – hupdy 2013-03-18 09:06:58

0

這是一個IndexOutOfBoundsException。當數組列表大小已經爲0時,即嘗試獲取第零個元素,即它是空的。檢查並調試你的代碼。

1

你叫

handToDraw.add(0, deck.get(0)); 

而不檢查,如果deck爲空。在下一行中,您將刪除一張卡片,因此它最終將變爲空白。

相關問題