超越Android编程中的数组

时间:2015-06-06 14:42:30

标签: java android arrays button

我在Android平台上写游戏。我有一系列按钮,当我点击其中一个按钮时,它会在它上面显示“X”和相邻的按钮,如下所示:

  X
X X X
  X

但是当我点击其中一个边框按钮时,应用程序正在停止。

这是我的方法,负责在按钮上设置文本(如果您需要更多代码,请告诉我):

private void changeState(int locationVer, int locationHor){
    String buttonText = BoardButtons[locationVer][locationHor].getText().toString();
    String rightButtonText = BoardButtons[locationVer+1][locationHor].getText().toString();
    String leftButtonText = BoardButtons[locationVer-1][locationHor].getText().toString();
    String lowerButtonText = BoardButtons[locationVer][locationHor+1].getText().toString();
    String upperButtonText = BoardButtons[locationVer][locationHor-1].getText().toString();
    if(locationVer == 0) {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }


        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }
    }

    else if(locationVer == 6) {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }


        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }
    }

    else if(locationHor == 0) {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
        }

        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
        }
    }

    else if(locationHor == 6) {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }

        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }
    }

    else {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }


        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }
    }
}

这是logcat显示的内容:

06-06 17:13:56.727    2232-2232/pl.edu.uksw.fieldsgame E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pl.edu.uksw.fieldsgame, PID: 2232
java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
        at pl.edu.uksw.fieldsgame.MainActivity.changeState(MainActivity.java:149)
        at pl.edu.uksw.fieldsgame.MainActivity.access$200(MainActivity.java:13)
        at pl.edu.uksw.fieldsgame.MainActivity$ButtonClick.onClick(MainActivity.java:137)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:0)

ArrayIndexOutOfBoundsException表示您正在尝试读取不在数组中的索引,在这种情况下,您有一个长度为7的数组(索引0到6),但是您要求的是位置7,哪个不存在。

我没有足够的数据来将行号与您的代码对齐,但我猜这是来自这一行:

String rightButtonText = BoardButtons[locationVer+1][locationHor].getText().toString();

在尝试从数组中读取locationVer+1之前,请不要检查以确保rightButtonText是有效的数组索引。看起来您的以下代码将处理nullString rightButtonText = null; if (locationVer + 1 < BoardButtons.length) { rightButtonText = BoardButtons[locationVer+1][locationHor].getText().toString(); } ,因此这应该有效(对每个数组读取重复,根据正在递增/递减的数组索引更改条件):

==

编辑:

您遇到的下一个问题是使用.equals() vs ==来比较字符串。在java中,对象(而不是基元)上使用的if (buttonText == String.valueOf(Game.PLAYER))运算符比较引用,而不是值(see here including answers for more explanation)。

当你致电buttonText时,你问的是String.valueOf(Game.PLAYER)false是同一个字符串(如“是相同的引用”,而不是“具有相同的值”)String.valueOf(Game.PLAYER) 。每次都会返回if (String.valueOf(Game.PLAYER).equals(buttonText)) ,因为{{1}}正在创建一个新字符串。要实际进行你认为正在进行的比较,将其交换为:

{{1}}