Creating Array Of Buttons

Discussion in 'Android Apps & Games' started by ramesh_bh, Oct 6, 2010.

  1. ramesh_bh

    ramesh_bh Member
    16

    Sep 16, 2010
    17
    1
    16
    HI ,

    I want to create buttons dynamically,
    My code is

    Button b1[] = null;

    for(int i=0;i<10;i++)
    {
    b1 = new Button(this);
    b1.setOnClickListener(this);
    - - - - - - - --
    - - - - - - --

    }

    But after running my code it is showing Null Pointer exception at creating the button. (new Button(this))
    Can anyone suggest how to create dynamically buttons,
    Please help
     

    Advertisement

  2. ianq62

    ianq62 New Member
    5

    Aug 21, 2011
    1
    0
    5
    I had a similar problem trying to create a dynamic array of checkboxes, so the number of array elements is undetermined until runtime. In order to solve this, I have to have a loop to determine the number of array elements.

    Then I had to allocate the array as follows:

    b1 = new Button[arrayCount];

    This will create an array of null buttons objects (in your example 10), which your for loop can then populate.

    I know it's almost a year since you posted, but hope this helps :)
     

Share This Page

Loading...