1

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

Ange
  • 309
  • 2
  • 5
  • 14

2 Answers2

2

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);
zapl
  • 63,179
  • 10
  • 123
  • 154
  • Thanks a lot I tried that and it works fine! But having a unusual problem with the program, it runs fine but when it's suppose to execute the grid activity, nothing happens leaving me with a blank screen! Any idea why that might be? [sorry for not posting the codes of the full program] – Ange Apr 17 '12 at 20:04
  • Do you get any errors or is there anything related in logcat? – zapl Apr 17 '12 at 20:38
  • Unfortunately no, nothing appears in the logcat – Ange Apr 17 '12 at 20:55
  • No idea what is wrong then. Maybe you don't set any layout at all / the wrong layout (setContentView). – zapl Apr 17 '12 at 20:59
  • As for the layout am using the "layoutVertical" as shown above. while also having a LinearLayout in xml!! is that right to do? – Ange Apr 17 '12 at 21:02
  • setContentView should be done only once in the Activity but the layout you set can have as much layouts in it as you want. – zapl Apr 17 '12 at 21:06
0

You are adding multiple tables instead of having one with many rows/columns. The flow should be something like this:

final LinearLayout layout = (LinearLayout)activity.findViewById(com.myapp.tableContainer);
final TableLayout table = new TableLayout(context);
layout.add(table, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

final TableRow row = new TableRow(context);

TextView textView = new TextView(context);
textView.setText("0");
textView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1));
row.addView(textView, 0);

textView = new TextView(context);
textView.setText("1");
textView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1));
row.addView(textView, 1);

table.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
               LayoutParams.MATCH_PARENT, 1));

And continue adding more rows the same way.

azertiti
  • 3,150
  • 17
  • 19