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);
}
}
}
檢查此鏈接:http://stackoverflow.com/questions/10007094/java-lang-illegalstateexception-the-specified-child-already-has-a-parent –
讓我試試看! – Badrinath