另一个布局我有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);
}
}
它崩溃了哪条线路?它在'row.addView(线性)'? –
其中3个addviews给出了错误? – gaurav5430
也不是你的线性== null吗?因为你没有膨胀它,它也不是为这个活动设置的contentView的一部分 – gaurav5430