【发布时间】: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