Creating a Button array


Last Updated:

  1. __Jon__

    __Jon__ Member This Topic's Starter

    Joined:
    Aug 16, 2010
    Messages:
    15
    Likes Received:
    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 (Text):
    1. int k = 0;
    2.         Button[] btnarray = new Button[new_word_array.length];
    3.  
    4.         while (k<new_word_array.length){
    5.          k++;
    6.          btnarray[k] = new Button(this);
    7.          layout.addView(btnarray[k]);
    8. }
    But it leads to errors. I can't find out to make button arrays properly in android. Any help?
     

    Advertisement
  2. mohitjaitly

    mohitjaitly Active Member

    Joined:
    Oct 18, 2010
    Messages:
    34
    Likes Received:
    0
    Any specific errors/exceptions ?

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

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     

Share This Page

Loading...