2013-03-13 4 views
0

이 코드에는 무슨 문제가 있습니까? 그것은 루프이기 때문에 당신은 ll.addView(ll2) 여러 번 전화하는거야동적 다른 레이아웃에 선형 레이아웃을 추가하십시오.

The specified child already has a parent. You must call removeView() on the child's parent first.

final LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    final LinearLayout ll2 = new LinearLayout(this); 
    ll2.setOrientation(LinearLayout.HORIZONTAL); 

     for(int i = 0; i < 20; i++) { 
      CheckBox cb = new CheckBox(getApplicationContext()); 
      TextView txt = new TextView(getApplicationContext()); 
       txt.setText("test!"); 
       ll2.addView(cb); 
       ll2.addView(txt); 
       ll.addView(ll2); //ERROR HERE 
      } 
     sc.addView(ll); 

답변

2

: 그것은 나에게이 오류를했다. for 루프 외부로 이동하십시오.

final LinearLayout ll = new LinearLayout(this); 
ll.setOrientation(LinearLayout.VERTICAL); 

final LinearLayout ll2 = new LinearLayout(this); 
ll2.setOrientation(LinearLayout.HORIZONTAL); 

for(int i = 0; i < 20; i++) { 
    CheckBox cb = new CheckBox(getApplicationContext()); 
    TextView txt = new TextView(getApplicationContext()); 
    txt.setText("test!"); 
    ll2.addView(cb); 
    ll2.addView(txt); 
} 

ll.addView(ll2); 
sc.addView(ll); 
관련 문제