由于未知 parent,无法添加视图?
Unable to addView because of unknown parent?
我正在尝试创建一个 table。但是,每次加载应用程序时,行数都会有所不同,因此我以编程方式添加它们。除了 mainLayout 之外,行有自己的 xml 布局。但是,当尝试向主布局中的 table 视图添加行时,出现错误 (tableLayou.addView(row))。这是我的代码:
private void makeTable() {
View v = getLayoutInflater().inflate(R.layout.row, null);
TableRow row;
AutofitTextView textView;
Configuration configuration = getResources().getConfiguration();
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 1, getResources().getDisplayMetrics());
int width = (configuration.screenWidthDp/8)*dip;
int height = (configuration.screenHeightDp/10)*dip;
v.setBackgroundColor(getResources().getColor(R.color.black));
for (int i = 0; i<rowss.size();i++) {
row =(TableRow) v.findViewById(R.id.tableRow);
Scanner scanner = new Scanner(rowss.get(i));
while(scanner.hasNextLine()) {
Scanner s = new Scanner(scanner.nextLine());
AutofitTextView textView1 = (AutofitTextView) row.findViewById(R.id.manufacturer);
textView1.setText(s.next());
}
tableLayout.addView(row);
}
}
Row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableRow"
android:weightSum="8">
<me.grantland.widget.AutofitTextView
android:id="@+id/manufacturer"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:id="@+id/name"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:id="@+id/reason"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/block"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/carrier"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/quantity"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/inventory"
android:layout_height="wrap_content" />
<CheckBox
android:layout_width="0dp"
android:id="@+id/status"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_main">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"/>
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
错误:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4654)
at android.view.ViewGroup.addView(ViewGroup.java:4490)
at android.widget.TableLayout.addView(TableLayout.java:426)
at android.view.ViewGroup.addView(ViewGroup.java:4431)
at android.widget.TableLayout.addView(TableLayout.java:408)
at android.view.ViewGroup.addView(ViewGroup.java:4404)
at android.widget.TableLayout.addView(TableLayout.java:399)
at com.intellidev.earlyupgradepicklist.MainActivity.makeTable(MainActivity.java:460)
at com.intellidev.earlyupgradepicklist.MainActivity.access0(MainActivity.java:64)
at com.intellidev.earlyupgradepicklist.MainActivity$MakeRequestTask.onPostExecute(MainActivity.java:411)
at com.intellidev.earlyupgradepicklist.MainActivity$MakeRequestTask.onPostExecute(MainActivity.java:331)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access0(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
首先。你的目标是什么?您希望将 TableLayout 用作列表?如果是这样,您可以而且应该为此目的使用 ListView。
The point of using ListView is to be able to scale to larger data sets
by not having to create and layout views for all of the items
up-front.
目前的问题是 每次 运行 循环时,您都会向 TableLayout object 添加相同的内容
row =(TableRow) v.findViewById(R.id.tableRow);
...
tableLayout.addView(row);
并且因为它已经存在,所以它会抛出“指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()'
您可以做的是拥有一个包含您创建的所有 TableRow 视图的 ArrayAdapter,并像这样添加它们:
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
tableLayout.addView(createTableRow(adapter.getItem(i)); // or tableLayout.addView(adapter.getView(i, null, tableLayout));
}
总的来说,您的 table_row.xml 看起来像是一个列表条目,您在其中有制造商、ID 等。
您可以将 TableLayout 切换为 ListView(这是处理行中嵌套数据的合适元素)
You can find documentation for the ListView here
和
Check this example for a ListView with CustomAdapter
无论如何,我很乐意为您提供帮助。
此致,
我正在尝试创建一个 table。但是,每次加载应用程序时,行数都会有所不同,因此我以编程方式添加它们。除了 mainLayout 之外,行有自己的 xml 布局。但是,当尝试向主布局中的 table 视图添加行时,出现错误 (tableLayou.addView(row))。这是我的代码:
private void makeTable() {
View v = getLayoutInflater().inflate(R.layout.row, null);
TableRow row;
AutofitTextView textView;
Configuration configuration = getResources().getConfiguration();
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 1, getResources().getDisplayMetrics());
int width = (configuration.screenWidthDp/8)*dip;
int height = (configuration.screenHeightDp/10)*dip;
v.setBackgroundColor(getResources().getColor(R.color.black));
for (int i = 0; i<rowss.size();i++) {
row =(TableRow) v.findViewById(R.id.tableRow);
Scanner scanner = new Scanner(rowss.get(i));
while(scanner.hasNextLine()) {
Scanner s = new Scanner(scanner.nextLine());
AutofitTextView textView1 = (AutofitTextView) row.findViewById(R.id.manufacturer);
textView1.setText(s.next());
}
tableLayout.addView(row);
}
}
Row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableRow"
android:weightSum="8">
<me.grantland.widget.AutofitTextView
android:id="@+id/manufacturer"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:id="@+id/name"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:id="@+id/reason"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/block"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/carrier"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/quantity"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/inventory"
android:layout_height="wrap_content" />
<CheckBox
android:layout_width="0dp"
android:id="@+id/status"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_main">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"/>
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
错误:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4654)
at android.view.ViewGroup.addView(ViewGroup.java:4490)
at android.widget.TableLayout.addView(TableLayout.java:426)
at android.view.ViewGroup.addView(ViewGroup.java:4431)
at android.widget.TableLayout.addView(TableLayout.java:408)
at android.view.ViewGroup.addView(ViewGroup.java:4404)
at android.widget.TableLayout.addView(TableLayout.java:399)
at com.intellidev.earlyupgradepicklist.MainActivity.makeTable(MainActivity.java:460)
at com.intellidev.earlyupgradepicklist.MainActivity.access0(MainActivity.java:64)
at com.intellidev.earlyupgradepicklist.MainActivity$MakeRequestTask.onPostExecute(MainActivity.java:411)
at com.intellidev.earlyupgradepicklist.MainActivity$MakeRequestTask.onPostExecute(MainActivity.java:331)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access0(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
首先。你的目标是什么?您希望将 TableLayout 用作列表?如果是这样,您可以而且应该为此目的使用 ListView。
The point of using ListView is to be able to scale to larger data sets by not having to create and layout views for all of the items up-front.
目前的问题是 每次 运行 循环时,您都会向 TableLayout object 添加相同的内容
row =(TableRow) v.findViewById(R.id.tableRow);
...
tableLayout.addView(row);
并且因为它已经存在,所以它会抛出“指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()'
您可以做的是拥有一个包含您创建的所有 TableRow 视图的 ArrayAdapter,并像这样添加它们:
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
tableLayout.addView(createTableRow(adapter.getItem(i)); // or tableLayout.addView(adapter.getView(i, null, tableLayout));
}
总的来说,您的 table_row.xml 看起来像是一个列表条目,您在其中有制造商、ID 等。 您可以将 TableLayout 切换为 ListView(这是处理行中嵌套数据的合适元素)
You can find documentation for the ListView here 和 Check this example for a ListView with CustomAdapter
无论如何,我很乐意为您提供帮助。 此致,