Получение недопустимого состояния Java Исключение при попытке добавить View, надувая xml

Em получает java.lang.IllegalStateException: указанный дочерний элемент уже имеет родителя. Сначала вы должны вызвать removeView() у родителя ребенка.

Не в состоянии выяснить, какой вид должен быть удален, любая помощь будет очень полезна.

Вот фрагмент кода

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);
        }

    }
}

1 ответ

Решение

Код ниже неверен:

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>
Другие вопросы по тегам