The 'activity_main' layout doesn't have any view with id R.id.linear_layout
. So the execution of the statement
LinearLayout linear = (LinearLayout) findViewById(R.id.linear_layout1);
will result in setting linear
to null
IllegalStateException when add a layout into another layout with LayoutInflater
-
21-09-2022 - |
質問
I have 3 layouts:
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>
layout1:
<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>
and layout2:
<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>
I want to add layout2 into layout1, after that, add layout1 into activity_main layout. Here my code:
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);
}
}
Anh I get a error:
java.lang.IllegalStateException: The specified child already has a parent. You must call remoteView() on the child's parent first
Please help me. Thanks for reading.
My solution: Thank all. I think I fixed it. Here my code:
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);
}
}
解決
他のヒント
Try this:
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);
}
}
所属していません StackOverflow