1. Introducing Channels - a new way to chat with other Android users!
    Dismiss Notice

Creating Array Of Buttons


Last Updated:

  1. ramesh_bh

    ramesh_bh Member This Topic's Starter

    Joined:
    Sep 16, 2010
    Messages:
    17
    Likes Received:
    1
    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

    Joined:
    Aug 21, 2011
    Messages:
    1
    Likes Received:
    0
    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...