Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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_Arrays - Fatal编程技术网

Java ArrayIndexOutOfBoundsException。安卓

Java ArrayIndexOutOfBoundsException。安卓,java,android,arrays,Java,Android,Arrays,我在一些设备上的google开发者控制台上发现ArrayIndexOutOfBoundsException崩溃。这对我来说很奇怪,因为它不会发生在我的设备和我已经测试过的其他设备上,我也在emulator上的不同版本的API上测试了我的apk,崩溃不会发生。我完全看不见应该怎么做才能避免这次崩溃,因为我看不见它以下是我的按钮阵列代码: public static Button[] button_array = null; int btn_ref = 0; int count = 0;

我在一些设备上的google开发者控制台上发现ArrayIndexOutOfBoundsException崩溃。这对我来说很奇怪,因为它不会发生在我的设备和我已经测试过的其他设备上,我也在emulator上的不同版本的API上测试了我的apk,崩溃不会发生。我完全看不见应该怎么做才能避免这次崩溃,因为我看不见它以下是我的按钮阵列代码:

public static Button[] button_array = null;
int btn_ref = 0;

int count = 0;
        for (int i = 0; i < words.size(); i++) {

            if (i >= 5) {

                for (int z = i; z < words.size(); z++) {
                    bonus_words.add(words.get(z));
                }
                if (!checkResult.equals("true")) {
                    tinydb.putListString("BonusList", bonus_words);
                }
                break;
            } else {
                char[] eachLetterinArray = words.get(i).toCharArray();
                for (int j = 0; j < eachLetterinArray.length; j++) {
                    if (Character.isLetter(eachLetterinArray[j])) {
                        count++;
                    }
                }
            }

        }

button_array = new Button[count];

 for (int i = 0; i < words.size(); i++) {
            int z = words.get(i).length();

            LinearLayout ln = new LinearLayout(mContext);
            ln.setOrientation(LinearLayout.HORIZONTAL);
            ln.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

            row = i;
            int row_items_count_start = 0;
            int row_items_count_end = 0;

            boolean first_time_check_row = true;

for (i = 0; i < z; i++) {

                row_items_count_end = btn_ref;
                if (first_time_check_row) {
                    row_items_count_start = btn_ref;
                    first_time_check_row = false;
                }

                button_array[btn_ref] = new Button(mContext); //**Getting crash here**

                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(width_height_cal, width_height_cal, 0.1f);
                p.setMargins(5, 5, 5, 5);
                button_array[btn_ref].setLayoutParams(p);
                button_array[btn_ref].setTextColor(Color.WHITE);
                button_array[btn_ref].setBackgroundResource(R.drawable.un_filled_box2);
                ln.addView(button_array[btn_ref]);

                btn_ref++;
            }

            indexes.add("row " + row + "," + row_items_count_start + "," + row_items_count_end + "," + words.get(i));
            vert_lay.setOrientation(LinearLayout.VERTICAL);
            vert_lay.addView(ln);

            if (indexes.size() >= 5) {
                break;
            }
公共静态按钮[]按钮数组=null;
int btn_ref=0;
整数计数=0;
for(int i=0;i=5){
for(intz=i;z=5){
打破
}

btn_ref
用于保存数组中按钮的索引,然后我将该按钮数组放入布局中

您必须使用大小初始化数组

int max_word_size = 20; //for example
public static Button[] button_array = new Button[max_word_size ];

您是否正在尝试创建关于按钮的
z
?我已经更新了我的问题。实际上,我有一个
单词的数组列表,我在顶部循环中一个接一个地提取单词,z包含每个单词的长度,在为单词创建按钮数组之后,我更新了我的代码。实际上,我得到了前5个entri单词数组的大小,然后获取每个单词的大小和每个字母的增量计数。button_数组具有传递给它的动态大小,即countarray具有固定大小。对于动态大小,请使用arraylist或使用最大假定大小的数组。使用此简单数组按钮[]button_数组=新建按钮[max_word_size]之间有何区别;和ArrayList按钮\u数组=新的ArrayList(最大单词大小)?不是都有固定大小,我们通过变量传递吗?数组有固定大小。我们不能在创建后更改它。但是arraylist我们可以删除项目,添加新项目,更改新任务的大小。arraylist更灵活。我不更改数组的大小。我在运行时给它一个固定大小,然后填充该数组。这个过程ds将在开发人员控制台上显示的某些设备上崩溃