2011-08-30 12 views
0

実行時に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>; 

ループではなく、:あなたのループ追加する前に

+0

があなたのlogcatを貼り付けます。例えば、buttonListがnullの場合があります(インスタンス化しましたか?) – IncrediApp

+0

logcatごとに、この行のCalculatorActivity.java:46にエラーが発生しているようです。 mainLayoutです。 addView(tblLayout); 私はbuttonListをインスタンス化しました。 –

答えて

0

TableRow tr = new TableRow(this); 

入れ:

tr.add(new TableRow(this)); 

そして最後にtr.get(r)してtrを交換してください。

TableRow tr = new TableRow(this); 
0

氏ハッピーゴーラッキーただ一つのことがonCreate()メソッドの先頭にsetContentView(R.layout.your Main Layout); text = (EditText)findViewById(R.id.editText1); beofre手段を記述します:あなたはあなたのとあなたのループの各ターンであなたのTRを消去し、ため、これらの変更のようなものは、あなたを助ける必要があります私たちはどんなsetContentViewなしsetContentView.

前に、任意のviewを定義カントので、あなたは、行の下findViewById(R.id.editText1);

0

は後の最初の1になるようになどの任意のビューを見つけるカント

setContentView(mainLayout); 

のみfindViewByid作品

関連する問題