【问题标题】:IllegalStateException when add a layout into another layout with LayoutInflater使用 LayoutInflater 将布局添加到另一个布局时出现 IllegalStateException
【发布时间】:2013-12-23 16:51:20
【问题描述】:

我有 3 个布局:

activity_main:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/table_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

</TableLayout>

布局1:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

和布局2:

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

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="a"/>

</LinearLayout>

我想将layout2添加到layout1中,然后将layout1添加到activity_main布局中。这是我的代码:

主活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TableLayout table = (TableLayout) findViewById(R.id.table_layout);
    LinearLayout linear = (LinearLayout) findViewById(R.id.linear_layout1);

    for (int i = 0; i < 8; i++) {
        View view = getLayoutInflater().inflate(R.layout.layout2, linear, false);
        linear.addView(view);
        TableRow row = new TableRow(getApplicationContext());
        row.addView(linear);
        table.addView(row);
    }
}

我得到一个错误:

java.lang.IllegalStateException: The specified child already has a parent. You must call remoteView() on the child's parent first

请帮助我。 感谢阅读。

我的解决方案: 谢谢大家。我想我修好了。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TableLayout table = (TableLayout) findViewById(R.id.tablelayout);
    for (int i = 0; i < 8; i++) {
        LinearLayout linear = (LinearLayout) getLayoutInflater().inflate(R.layout.layout1, table, false);
        View view = getLayoutInflater().inflate(R.layout.layout2, linear, false);
        linear.addView(view);
        TableLayout row = new TableLayout(this);
        row.addView(linear);
        table.addView(row);
    }
}

【问题讨论】:

  • 哪一行崩溃了?是在row.addView(linear)吗?
  • 3 个 addviews 中哪一个会出错?
  • 也不是你的线性==null吗?因为你没有给它充气,而且它也不是为此活动设置的 contentView 的一部分
  • 是的,我的问题是我的线性 == null。你能帮我修一下吗?

标签: android layout illegalstateexception


【解决方案1】:

“activity_main”布局没有任何 ID 为 R.id.linear_layout 的视图。所以语句的执行 LinearLayout linear = (LinearLayout) findViewById(R.id.linear_layout1); 将导致将linear 设置为null

【讨论】:

  • 但我没有任何解决办法。你能帮帮我吗?
  • @hungduong 你想把 layout2 的 8 个实例放在 layout1 的 LinearLayout 中吗?
  • 我想将layout2的8个实例放入layout1,然后将layout1放入activity_main布局中。
  • 我想我修好了。我的解决方案在我的问题中:)
【解决方案2】:

试试这个:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TableLayout table = (TableLayout) findViewById(R.id.table_layout);
    LinearLayout linear = getLayoutInflater().inflate(R.layout.layout1, table, false);

    for (int i = 0; i < 8; i++) {
        View view = getLayoutInflater().inflate(R.layout.layout2, linear, false);
        linear.addView(view);
        TableRow row = new TableRow(getApplicationContext());
        row.addView(linear);
        table.addView(row);
    }
}

【讨论】:

    猜你喜欢
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多