I am getting crashes of ArrayIndexOutOfBoundsException on google developer console on some devices. It is strange for me because it does not happen on my device and some others which i have tested and i also tested my apk on different versions of API'S on emulator and crash does not happen. I am totaly blind what should i do to avoid this crash as i am unable to see it.Getting crash on button_array[btn_ref] = new Button(mContext);
Attached below is my code for button array:
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;
}
btn_ref
is used to save index of a button in array and after that i am putting that button array into a layout