我有这段代码,我想在 LinearLayout 中动态添加 CheckBoxes,该复选框嵌套在嵌套在 RelativeLayout 中的 ScrollView 中(RelativeLayout->ScrollView->LinearLayout->My ChechBoxes)
li = (RelativeLayout) findViewById(R.id.mainlayout);
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
li.addView(sv);
sv.addView(ll);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
this.setContentView(sv);
但我收到此错误:
03-12 20:32:14.840: E/AndroidRuntime(945): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我在我的 XML 文件中声明的 RelativeLayout 已经如何解决这个问题?