0

我被卡住了,运行 GameView 类时出现错误。它编译没有错误,但是在 android 仿真上运行它时会崩溃。我将在代码之后发布 logcat。

代码

    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)

任何帮助将不胜感激。

4

3 回答 3

3

initCards()永远不会在任何地方调用,因此卡组列表是空的,因此调用handToDraw.add(0, deck.get(0))drawCard()导致您遇到异常,这清楚地表明了问题:您正在尝试访问大小为 0 的列表的索引 0 处的元素:

Invalid location 0, size is 0
于 2013-03-18T08:23:40.227 回答
1

你打电话

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

不检查是否deck为空。在下一行你

deck.remove(0);

取出一张卡,所以它最终会变空。

于 2013-03-18T08:25:02.447 回答
0

这是一个 IndexOutOfBoundsException。您正在尝试获取第零个元素,此时数组列表大小已经为 0,即它为空。检查并调试您的代码。

于 2013-03-18T08:24:12.440 回答