Aamir February 2016

How do you add a button inside of a for loop?

I'm trying to make a program that asks the user for a number through an Alert Dialog(That part is complete) but then I would like to generate as many buttons as the user specified. I tried to make a for loop to create the buttons, but I can't get it to work properly:

    Button[]buttons;
    buttons = new Button[numOfButtons];
    for(int i = 1;i!=numOfButtons;i++){
        buttons[i-1] = new Button(this);
        layout1.addView(buttons[i-1]);

    }

The only logical way that I thought I could get this to work was to use an array since I don't believe you can keep declaring new variables inside a loop, but this doesn't seem to work. Sorry if this is bit silly, I'm still new to all this. It gives a fatal exception error.

Full Code:

public class MainActivity extends AppCompatActivity {
private Editable numButtons;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RelativeLayout layout1 = new RelativeLayout(this);
    setContentView(layout1);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Enter the number of buttons: ");

    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    builder.setView(input);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            numButtons = input.getText();
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.show();

    Button[]buttons;
    int numOfButtons = Integer.parseInt(numButtons.toString());
    buttons = new Button[numOfButtons];

    for(int i = 1; i != numOfButtons; i++){
        buttons[i-1] = new Button(this);
        layo        

Answers


cricket_007 February 2016

The AlertDialog button click is asynchronous so as soon as the code hits this line...

int numOfButtons = Integer.parseInt(numButtons.toString());

you will get a NullPointerException because numButtons is null at that point.

Please try this instead. Assuming the layout1.addView works as it should. Note, you'll need to make layout1 final or move it out as a class field to reference the variable here.

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        numButtons = input.getText();

        int numOfButtons = Integer.parseInt(numButtons.toString());
        Button[] buttons = new Button[numOfButtons];

        for(int i = 0; i < numOfButtons; i++){
            buttons[i] = new Button(MainActivity.this);
            layout1.addView(buttons[i]);
        }
    }
});

Post Status

Asked in February 2016
Viewed 1,428 times
Voted 9
Answered 1 times

Search




Leave an answer