2013-12-23 32 views
0

另一个布局我有3个布局:IllegalStateException异常时添加布局与LayoutInflater

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布局中。在这里我的代码:

MainActivity:

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

它崩溃了哪条线路?它在'row.addView(线性)'? –

+0

其中3个addviews给出了错误? – gaurav5430

+0

也不是你的线性== null吗?因为你没有膨胀它,它也不是为这个活动设置的contentView的一部分 – gaurav5430

回答

0

的 'activity_main' 布局没有ID为R.id.linear_layout任何观点。因此,声明 LinearLayout linear = (LinearLayout) findViewById(R.id.linear_layout1); 的执行将导致设置linearnull

+0

但我没有任何解决方案。你可以帮我吗? – hungduong

+0

@hungduong您是否想在layout1的LinearLayout中放置8个layout2实例? – GareginSargsyan

+0

我想将layout2的8个实例放入layout1中,之后将layout1放入activity_main布局中。 – hungduong

0

试试这个:

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

我试过了,但它失败:( – hungduong

相关问题