表行指定的子项已有父项。您必须首先在孩子的父母上调用removeView()

时间:2014-11-09 07:03:23

标签: android tablelayout android-tablelayout tablerow

无法在表格布局中实际添加表格行。我在这里创建了一个可水平和垂直滚动的视图,并且视图类似于表格,添加TableRow

时出现问题
/*
 *  reportLayout is tablelayout by findViewById
 *  i am setting view inside Fragment
 */
public void redrawReport(){
    reportLayout.removeAllViews();
    int count=0;
    for(SaleReportModel model:Detailist.getReportItems()){
        TableRow headerRow= new TableRow(getActivity());
        headerRow.removeAllViews();

        //Error Caused here in this line

        headerRow.addView(getTextView(model.getPrdcode(), false,count*4),count);
        headerRow.addView(getTextView(model.getSubtype(), false,count*4+1),count);
        headerRow.addView(getTextView(""+model.getDispatchQty(), false,count*4+2),count);
        headerRow.addView(getTextView(""+model.getDispatchvalue(), false,count*4+3),count);
        reportLayout.addView(headerRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT));
        count+=1;
    }
}
public TextView getTextView(String data,boolean isHeader,int id){
    TextView textView= new TextView(getActivity());
    textView.setLayoutParams( new LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT));
    textView.setText(data);
    textView.setId(id);
    textView.setGravity(Gravity.CENTER);
    //textView.setTextColor(Color.BLACK);
    return mDateFrom;

}

错误日志:

11-09 12:04:40.299: E/AndroidRuntime(19569): FATAL EXCEPTION: main
11-09 12:04:40.299: E/AndroidRuntime(19569): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
11-09 12:04:40.299: E/AndroidRuntime(19569):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3739)
11-09 12:04:40.299: E/AndroidRuntime(19569):    at android.view.ViewGroup.addView(ViewGroup.java:3610)
11-09 12:04:40.299: E/AndroidRuntime(19569):    at android.view.ViewGroup.addView(ViewGroup.java:3555)
11-09 12:04:40.299: E/AndroidRuntime(19569):    at com.kiss.erp.view.fragment.SalesFragment.redrawReport(SalesFragment.java:254)
11-09 12:04:40.299: E/AndroidRuntime(19569):    at com.kiss.erp.view.fragment.SalesFragment.showdetail(SalesFragment.java:196)
11-09 12:04:40.299: E/AndroidRuntime(19569):    at com.kiss.erp.view.fragment.SalesFragment.onClick(SalesFragment.java:110)

请帮忙。

可能是由TableLayout的父级引起的,所以在这里查看xml。

<ScrollView 

android:id="@+id/layoutReport" 
android:layout_height="match_parent"         
android:scrollbars="horizontal|vertical" 
android:layout_width="match_parent"     
android:layout_marginTop="5dip"     
android:scrollbarStyle="outsideInset"
android:fillViewport="true"> 

<HorizontalScrollView 
    android:id="@+id/horizontalView" 
    android:layout_height="wrap_content"     
    android:scrollbars="horizontal|vertical" 
    android:layout_width="wrap_content"     
    android:layout_marginTop="5dip">

    <TableLayout
        android:id="@+id/tlGridTable"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
</TableLayout>
</HorizontalScrollView>

提前致谢。

1 个答案:

答案 0 :(得分:4)

在方法getTextView中,您返回mDateForm而不是textView。 由于mDateForm已有父视图,因此您将获得异常。 正确的实现应该是这样的:

public TextView getTextView(String data,boolean isHeader,int id){
    TextView textView= new TextView(getActivity());
    textView.setLayoutParams( new  LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT));
    textView.setText(data);
    textView.setId(id);
    textView.setGravity(Gravity.CENTER);
    //textView.setTextColor(Color.BLACK);
    return textView;

}