1. Download our Official Android App: Forums for Android!

Apps Creating a Button array

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

  1. __Jon__

    __Jon__ Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    15
    Joined:
    Aug 16, 2010

    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 Member
    Rank:
    None
    Points:
    15
    Posts:
    34
    Joined:
    Oct 18, 2010

    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 Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    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...