• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Creating Array Of Buttons

ramesh_bh

Newbie
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
 
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 :-)
 
Back
Top Bottom