【问题标题】:Getting Java illegal state Exception when trying to add View by inflating xml尝试通过膨胀 xml 添加视图时出现 Java 非法状态异常
【发布时间】:2012-10-15 07:31:17
【问题描述】:

Em 得到 java.lang.IllegalStateException: 指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。

无法找出必须删除哪个视图,任何帮助都会非常有帮助。

这里是代码 sn-p

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>

数据.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);
        }

    }
}

【问题讨论】:

标签: android android-relativelayout layout-inflater illegalstateexception


【解决方案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>

【讨论】:

  • 正确!当我尝试您编辑的代码时它正在工作,但文本重叠。即使我使用参数进行相对布局。检查编辑的代码
  • @Badrinath 这是因为您在main.xml 文件中使用RelativeLayout 而不是LinearLayout 并将orientation 设置为vertical
  • 我需要在另一个下方添加多个文本视图,因此我使用了充气机制。
  • 按照你所说的改变后,它就像魅力一样!
猜你喜欢
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多