0

Em獲取java.lang.IllegalStateException:指定的子項已具有父項。您必須先調用子對象的父對象的removeView()。獲取Java非法狀態嘗試添加視圖時通過膨脹xml導致異常

無法找出哪個視圖已被刪除,任何幫助將非常有幫助。

這裏是代碼片段

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:id="@+id/mainLayout" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

</RelativeLayout> 

data.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

</RelativeLayout> 

活動代碼

public class ToDo extends Activity { 
    /** Called when the activity is first created. */ 
    Button addNew; 
    RelativeLayout mainLayout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mainLayout=(RelativeLayout)findViewById(R.id.mainLayout); 

     RelativeLayout rel; 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     for(int idx=0;idx<2;idx++){ 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

      rel = (RelativeLayout) inflater.inflate(R.layout.data,null); 
      params.setMargins(0, 50, 0, 0); 

      TextView fromWeb= (TextView) rel.findViewById(R.id.txt); 
      fromWeb.setText("AA"); 

      mainLayout.addView(rel,params); 
     } 

    } 
} 
+0

檢查此鏈接:http://stackoverflow.com/questions/10007094/java-lang-illegalstateexception-the-specified-child-already-has-a-parent –

+0

讓我試試看! – Badrinath

回答

0

下面的代碼是不正確:

TextView fromWeb= (TextView) rel.findViewById(R.id.txt); 
fromWeb.setText("AA"); 
rel.addView(fromWeb,params); // the TextView is alredy in the rel RelativeLayout! 
mainLayout.addView(rel); 

,因爲你要添加再次(與addView法)TextView這已經是在佈局文件(如你之前搜索它findViewById)。相反,它應該是這樣的:

TextView fromWeb= (TextView) rel.findViewById(R.id.txt); 
fromWeb.setText("AA"); 
mainLayout.addView(rel); 

如果你想爲你的TextView然後將它們設置在佈局文件邊距:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:text="TextView" /> 

</RelativeLayout> 
+0

正確!當我嘗試你的編輯代碼它正在工作,但文本是overlaping.even雖然我使用params相對佈局。檢查編輯後的代碼 – Badrinath

+0

@Badrinath這是因爲在'main.xml'文件中使用'RelativeLayout'而不是'orientation'設置爲'vertical'的'LinearLayout'。 – Luksprog

+0

我需要添加多個文本視圖一個在另一個下面,所以我使用充氣機制。 – Badrinath