• 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
Sep 16, 2010
17
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
 
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 :)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones