在 Android 编程中超越数组

Going beyond the array in Android programming

我正在 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.access0(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)

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

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

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

在尝试从数组中读取它之前,您没有检查以确保 locationVer+1 是一个有效的数组索引。看起来您的以下代码将处理 rightButtonTextnull,因此这应该有效(对每个数组读取重复,根据数组索引更改条件为 incremented/decremented):

String rightButtonText = null;
if (locationVer + 1 < BoardButtons.length) {
    rightButtonText = BoardButtons[locationVer+1][locationHor].getText().toString();
}

编辑:

下一个问题是使用 ==.equals() 比较字符串。在 java 中,对象(非基元)上使用的 == 运算符比较引用,而不是值 (see here including answers for more explanation)。

当您调用 if (buttonText == String.valueOf(Game.PLAYER)) 时,您是在询问 java buttonText 是否与 buttonText 中的字符串相同(如 "is the same reference",而不是 "has the same value") =22=]。每次都会 return false 因为 String.valueOf(Game.PLAYER) 正在创建一个新字符串。要实际进行您认为得到的比较,请将其换成:

if (String.valueOf(Game.PLAYER).equals(buttonText))