• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Creating a Button array

__Jon__

Newbie
Aug 16, 2010
15
0
I'm trying to create a button array, where the number of buttons depends on the length of a char array.

This is my code:
Code:
int k = 0;
        Button[] btnarray = new Button[new_word_array.length];
 
        while (k<new_word_array.length){
         k++;
         btnarray[k] = new Button(this);
         layout.addView(btnarray[k]);
}

But it leads to errors. I can't find out to make button arrays properly in android. Any help?
 
Any specific errors/exceptions ?

plus increment k after adding new Button object to the array.

Yes, I am willing to bet that you are getting an ArrayOutOfBoundsException, because you are incrementing k BEFORE you do any initializing, so the last run-through of the while loop is trying to initialize a member of the array that does not exist.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones