2011-07-08 3 views
0

私は、動的タブロー/テーブルレイアウトから生成された1つの画面上に2つの領域を作成しようとしています。Android TableLayout - "指定された子はすでに親を持っています。最初に親のremoveView()を呼び出す必要があります。"

最初のtablelayoutには6つのtextview要素を持つtablerowがあります。

第2のテーブルレイアウトには、それぞれ6つのボタンがある2つのタブローがあります。

各ボタン/テキストビューはxmlファイルから購入されます。

問題この2つのテーブルを実行しようとすると、titleに記載されているエラーがスローされていることがあります。あなたの時間のために、以下の行

tablerow.addView(View1); 

CODE

Layout1 = (TableLayout) findViewById(R.id.scoring_tableLayout1); 
Layout2 = (TableLayout) findViewById(R.id.scoring_tableLayout2); 

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

text = new TextView[6]; 
TableRow tablerow1 = new TableRow(this); 

for (int i = 0; i < 6; i++) { 
    View1 = (View) inflater.inflate(R.layout.layout1, null); 
    text[i] = (TextView) endValueView.findViewById(R.id.textView_item); 
    tablerow.addView(View1); 
} 
Layout1.addView(tablerow1); 

button = new Button[12]; 

for (int i = 0; i < 2; i++) { 
    TableRow tablerow2 = new TableRow(this); 
    for (int j = 0; j < 6; j++) { 
     ButtonView = (View) inflater.inflate(R.layout.button, null); 
     button[j] = (Button) ButtonView 
       .findViewById(R.id.scoring_arrow_score_button); 
     button[j].setText("some text"); 
     tablerow2.addView(ButtonView); 
    } 
    Layout2.addView(tablerow2); 
} 

おかげでエラーを.IT

答えて

0

私はあなたがループ内で同じtextView_itemを呼び出しているので、それがあると思います。

テキスト[I] =(のTextView)endValueView.findViewById(R.id.textView_item)。

textViewとButtonViewの名前はどのようにして指定しますか?

理想的には、何を行うことができますが..... tvItem1、tvItem2..etcとして各textVeiwとButtonViewのIDを設定し

このlinkに行ってください、ここであなたはどのように見つけるだろうさループ内でfindViewByIdを実行します。

+0

私は2番目のループ内で同じidボタンを呼び出しています。それはtextviewsのために異なって動作しますか? –

0

コンポーネントを膨張させると、コードが実行されているビューにコンポーネントが自動的に追加されます。 たとえば、

public calss MyClass extends LinearLayout { 
... 
private void init() { 
    inflater.inflate(R.layout.button, null); 
    ... 

の場合、LinearLayout(MyClass)にビジュアルコンポーネント(ボタン)を追加します。 そのため、同じボタンをループして2回膨張させると、コンポーネントにはすでに追加されているというエラーが表示されます。

あなたが実際に使用したいのは、使用しているインフレータの代わりにfindViewByIdです。

幸運を祈る!

関連する問題

 関連する問題