How to add table rows to table layout programmatically? : <p>Hi I am trying to add some TableRows to a TableLayout, the TableLayout is declared within the xml-file, the TableRows are intantiated only in the java code.
Altough there are some similar questions, I could not make it work in my code:</p>

<pre><code>    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TableRow.LayoutParams;

    public class MainActivity extends Activity {
    TableLayout tableLayout;
    Button[][] buttons =new Button[9][9];
    TableRow tableRow[]=new TableRow[9];
    TableRow.LayoutParams layoutParams=new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tableLayout = (TableLayout) findViewById(R.id.tablay);
    for(int line=0; line&lt;9; line++){
        tableRow[line]=new TableRow(this);
        tableRow[line].setLayoutParams(layoutParams);
        tableRow[line].setVisibility(View.VISIBLE);
    }
    for(int line=0; line&lt;9; line++){
        for(int col=0; col&lt;9; col++){
            buttons[line][col]=new Button(this);
            buttons[line][col].setText(""+line+col);
            buttons[line][col].setLayoutParams(layoutParams);
            buttons[line][col].setVisibility(View.VISIBLE);
            tableRow[line].addView(buttons[line][col]);
        }
    }
    for(int i=0; i&lt;9; i++){
        tableLayout.addView(tableRow[i], new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
setContentView(R.layout.activity_main);
}
}
</code></pre>

<p>The app always terminates immediately after launching, I can not see what the problem is. 
Thanks for your help!</p>
