我遇到此错误。
java.lang.illegalstateException:指定的孩子已经有一个 父母。您必须首先在孩子的父母上调用removeview()。
来自以下代码:
myActivity.java
public void displayTable(){//This is method to display table
List<Integer> lstIds = helper.getAllTimeOffID(); // it will fetch number of records
for(int id:lstIds){
//for each record i am generationg single row(LinearLayout[horizontal])
TimeOff timeOff = helper.getTimeOffInfo(id);//this is model class for each record
lstAllTimeOff.add(timeOff); // adding to list for future purpose(no need for this mithod)
int pxs = this.getApplicationContext().getResources().getDimensionPixelSize(R.dimen.generalTextSize);
//////////////////////////////////////////
LinearLayout viewTimeOffDetailsArea = (LinearLayout) this.findViewById(R.id.viewTimeOffDetailsArea);
LinearLayout viewSingleRow = new LinearLayout(getApplicationContext()); //new row generating
LayoutParams singleRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
viewSingleRow.setOrientation(LinearLayout.HORIZONTAL);
TextView lblSingleRowDetail = new TextView(getApplicationContext());
TextView lblSingleRowDate = new TextView(getApplicationContext());
TextView lblSingleRowHours = new TextView(getApplicationContext());
ImageButton imgbtnSingleRowAction = new ImageButton(getApplicationContext());
///////////////////////////////////////////////////////////////////////
viewSingleRow.removeAllViews();
lblSingleRowDetail.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 3f));
lblSingleRowDate.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 2f));
lblSingleRowHours.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
lblSingleRowDetail.setTextSize(pxs);
lblSingleRowDate.setTextSize(pxs);
lblSingleRowHours.setTextSize(pxs);
lblSingleRowDetail.setText(timeOff.getTimeOffDetail());
lblSingleRowDate.setText(timeOff.getTimeOffDate());
lblSingleRowHours.setText(timeOff.getTimeOffHours());
imgbtnSingleRowAction.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imgbtnSingleRowAction.setImageResource(R.drawable.delete1);
imgbtnSingleRowAction.setId(timeOff.getId());
viewSingleRow.addView(lblSingleRowDetail);//add label
viewSingleRow.addView(lblSingleRowDate);//add label
viewSingleRow.addView(lblSingleRowHours);//add label
viewSingleRow.addView(imgbtnSingleRowAction);//add image button
//add whole row into vertical Linear Layout which is in Scroll View
viewTimeOffDetailsArea.addView(lblSingleRowDetail); //on this line i am getting Error
}
这是我试图一个一个a行的XML文件
my_xml.xml
<ScrollView
android:id="@+id/scrollDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/viewTimeOffDetailsFooter"
android:layout_below="@id/lowerSeparator" >
<LinearLayout
android:id="@+id/viewTimeOffDetailsArea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
您正在调用viewSingleRow.addView(lblSingleRowDetail);
,然后viewTimeOffDetailsArea.addView(lblSingleRowDetail);
会导致异常。您只能将视图添加到一个父母。