Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是什么导致此ArrayIndexOutOfBoundsException?_Java_Android_Indexoutofboundsexception - Fatal编程技术网

Java 是什么导致此ArrayIndexOutOfBoundsException?

Java 是什么导致此ArrayIndexOutOfBoundsException?,java,android,indexoutofboundsexception,Java,Android,Indexoutofboundsexception,我正在为Android做一个数独游戏,但我遇到了一个问题。游戏会编译,但一旦你进入游戏屏幕并按下按钮,游戏就会崩溃 我检查了日志,这些似乎是错误: 05-04 09:07:41.620: ERROR/AndroidRuntime(325): FATAL EXCEPTION: main 05-04 09:07:41.620: ERROR/AndroidRuntime(325): java.lang.ArrayIndexOutOfBoundsException 05-04 09:07:41.620:

我正在为Android做一个数独游戏,但我遇到了一个问题。游戏会编译,但一旦你进入游戏屏幕并按下按钮,游戏就会崩溃

我检查了日志,这些似乎是错误:

05-04 09:07:41.620: ERROR/AndroidRuntime(325): FATAL EXCEPTION: main
05-04 09:07:41.620: ERROR/AndroidRuntime(325): java.lang.ArrayIndexOutOfBoundsException
05-04 09:07:41.620: ERROR/AndroidRuntime(325):     at org.example.fpsudoku.Button.findScreenV(Button.java:59)
05-04 09:07:41.620: ERROR/AndroidRuntime(325):     at org.example.fpsudoku.Button.onCreate(Button.java:38)
05-04 09:07:41.620: ERROR/AndroidRuntime(325):     at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
05-04 09:07:41.620: ERROR/AndroidRuntime(325):     at android.app.Dialog.show(Dialog.java:225)
05-04 09:07:41.620: ERROR/AndroidRuntime(325):     at org.example.fpsudoku.Game.showButtonOrError(Game.java:181)
05-04 09:07:41.620: ERROR/AndroidRuntime(325):     at org.example.fpsudoku.SudokuScreen.onTouchEvent(SudokuScreen.java:221)
05-04 09:07:41.620: ERROR/AndroidRuntime(325):     at android.view.View.dispatchTouchEvent(View.java:3766)
这就是问题所在的代码:

    findScreenV();

    for (int element : usesquare)
    {
        if (element != 0)
            buttons[element - 1].setVisibility(View.INVISIBLE);
    }
    setListeners();
}

private void findScreenV()
{
    button = findViewById(R.id.button);
    buttons[1] = findViewById(R.id.button_1);
    buttons[2] = findViewById(R.id.button_2);
    buttons[3] = findViewById(R.id.button_3);
    buttons[4] = findViewById(R.id.button_4);
    buttons[5] = findViewById(R.id.button_5);
    buttons[6] = findViewById(R.id.button_6);
    buttons[7] = findViewById(R.id.button_7);
    buttons[8] = findViewById(R.id.button_8);
    buttons[9] = findViewById(R.id.button_9);

只是一个猜测-数组索引是基于零的。如果你需要9个按钮,你就可以了

Button[] buttons = new Button[9];
创建数组和

button = findViewById(R.id.button);
buttons[0] = findViewById(R.id.button_1);
buttons[1] = findViewById(R.id.button_2);
buttons[2] = findViewById(R.id.button_3);
buttons[3] = findViewById(R.id.button_4);
buttons[4] = findViewById(R.id.button_5);
buttons[5] = findViewById(R.id.button_6);
buttons[6] = findViewById(R.id.button_7);
buttons[7] = findViewById(R.id.button_8);
buttons[8] = findViewById(R.id.button_9);

来填充它。在我的示例中,寻址
按钮[9]
将在Java中创建一个
数组索引OutofBoundsException

,在创建数组时,您需要给出该数组的固定大小。 like Button[]buttons=new Button[9],然后创建该数组的对象,like buttons[0]=findViewById(R.id.Button_1);
另一种方法是使用arrayList作为最佳解决方案。

如果数组的长度为9,则表示如果尝试检索arr[9],则可以检索arr[0]到arr[8]如果没有,您将获得ArrayIndexOutOfBoundsException。

我们忽略了两件重要的事情-按钮数组的大小和usesquare的大小?您建议我将该函数放在我的文件中的什么位置?@rich-errr-第二个片段是源代码的正确副本,只需将其粘贴回代码中即可(
findScreenV
method)。