Q:

In android how do I add buttons into a TableLayout from an array of buttons programmatically? 

Am new to android programming, just started learning it the past 6 weeks and am writing a minesweeper game for android, well i've managed to do some part of the game without much issues.  However, I've got to design a grid programmatically using TableLayout and TableRow and insert buttons in them; so I've written few lines of code to do that but whenever I run the game i get "Confirm Perspective Switch" error. 
Here are the codes I've written - 
` public class Game extends Activity implements OnClickListener {

        Button[][] btn = new Button[6][6]; 
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gamegrid);

            int i, j;

            LinearLayout layoutVertical = (LinearLayout) findViewById(R.layout.gamegrid);
            //create a new TableLayout
            TableLayout table = null;

            table.setStretchAllColumns(true);  
            table.setShrinkAllColumns(true);

            LayoutParams param = new TableLayout. LayoutParams(LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT);

            for(i = 0; i <6; i++){
                table = new TableLayout(this);
                table.setWeightSum(5);
                layoutVertical.addView(table, param);
                for(j=0; j<7; j++){
                    btn[i][j] = new Button(this);
                    table.addView(btn[i][j], param);    
                    btn[i][j].setOnClickListener(this);
                    }
            } return;   
        }
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        }

    } `

I think my problem is with the following lines - 
`for(i = 0; i <6; i++){
table = new TableLayout(this);
    table.setWeightSum(5);
    layoutVertical.addView(table, param);
    for(j=0; j<7; j++){
        btn[i][j] = new Button(this);
        table.addView(btn[i][j], param);
        btn[i][j].setOnClickListener(this);
        }
    }`

It's suppose to create buttons then store them in an array of buttons then insert the buttons in the TableLayout! 
And why am I getting the above error? 
Could you please help me point out what am doing wrong?  As I do not have any errors showing. 
Thanks

A:

If you want to use a TableLayout then what you need to construct looks like this (using a 4x4 grid as example)
TableLayout
    TableRow
        Button
        Button
        Button
        Button
    TableRow
        Button
        Button
        Button
        Button
    TableRow
        Button
        Button
        Button
        Button
    TableRow
        Button
        Button
        Button
        Button

1 TableLayout that contains 4 TableRows and each row contains 4 Buttons (a 4x4 grid). 
In code it would like so maybe:
Button[][] buttonArray = new Button[4][4];
TableLayout table = new TableLayout(context);
for (int row = 0; row < 4; row++) {
    TableRow currentRow = new TableRow(context);
    for (int button = 0; button < 4; button++) {
        Button currentButton = new Button(context);
        // you could initialize them here
        currentButton.setOnClickListener(listener);
        // you can store them
        buttonArray[row][button] = currentButton;
        // and you have to add them to the TableRow
        currentRow.addView(currentButton);
    }
    // a new row has been constructed -> add to table
    table.addView(currentRow);
}
// and finally takes that new table and add it to your layout. 
layoutVertical.addView(table);

