2015-05-18 60 views
-1

我收到此错误。Android:指定的孩子已经有父母问题

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 

     } 

而这正是我试图画马行逐一
我的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> 
+1

为什么-1请你解释一下? – GreenRobo

回答

3

要调用viewSingleRow.addView(lblSingleRowDetail);然后viewTimeOffDetailsArea.addView(lblSingleRowDetail);它会导致异常。您只能将视图添加到一位家长。

+1

我是一个傻瓜家伙... – GreenRobo

+0

你能帮我解决同一个代码中的另一个问题吗? – GreenRobo

+0

@GreenRobo当然,问题是什么? –

相关问题