1. Check out our app, Forums for Android! Download and leave feedback here!

Creating a Button array

Discussion in 'Application Development' started by __Jon__, Oct 17, 2010.

  1. __Jon__

    __Jon__ Member
    Thread Starter
    15

    Aug 16, 2010
    15
    0
    15
    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
    15

    Oct 18, 2010
    34
    0
    15
    India
    Any specific errors/exceptions ?

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

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    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...