実行時にmainLeararLayoutにtableLayoutを追加しようとしています。 main.xmlにeditText(R.id.editText1)を追加しました。ここに私のコードです。動いていない。ランタイムエラーが発生する(アプリケーションが予期せず停止した)。Android - 実行時にメインレイアウトにレイアウトを追加する
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = (EditText)findViewById(R.id.editText1);
TableLayout tblLayout = new TableLayout(this);
tblLayout.setLayoutParams(new TableLayout.LayoutParams(8,5));
tblLayout.setPadding(1,1,1,1);
for(int r=0; r<ROW_COUNT; ++r)
{
TableRow tr = new TableRow(this);
for(int c=0; c<COL_COUNT; ++c)
{
int index = r * COL_COUNT + c;
buttonList.add(new Button(this));
buttonList.get(index).setText(buttonNames[index]);
tr.addView(buttonList.get(index), 60, 30);
}
tblLayout.addView(tr);
}
LinearLayout mainLayout = (LinearLayout)findViewById(R.layout.main);
mainLayout.addView(tblLayout);
setContentView(mainLayout);
}
いずれのポインタもわかります。ありがとう。
Arraylist<TableRow> tr = new ArrayList<TableRow>;
ループではなく、:あなたのループ追加する前に
があなたのlogcatを貼り付けます。例えば、buttonListがnullの場合があります(インスタンス化しましたか?) – IncrediApp
logcatごとに、この行のCalculatorActivity.java:46にエラーが発生しているようです。 mainLayoutです。 addView(tblLayout); 私はbuttonListをインスタンス化しました。 –