对于以下情况,我收到了specified child already has a parent. You must call removeView() on the child's parent first
的错误,看看您是否可以提供任何帮助!谢谢!
ScrollView
为4 TableLayout
,例如ex_1_list,ex_2_list等。
有4种组,我想在数据库中向相应的TableLayout
显示不同类别的数据,但如果数据库为空,那么它可能会在4个Tablelayout中的每一个中膨胀一个显示没有的视图每个类别的数据。
首先获取数据库大小,如果size = 0,则表示练习数据库中没有数据,它将转到else循环,如下所示:
int i = exercises.size();
for (int j = 0; j < i; j++)
{
Inflate_All_Ex_Data(j);
}
if (i==0)
{
Inflate_All_Ex_Data (0);
}
private void Inflate_All_Ex_Data (int index)
{
...
else
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newTagView = inflater.inflate(R.layout.exercises_info, null);
Button exercise_id = (Button) newTagView.findViewById(R.id.exercise_id);
Button exercise_group = (Button) newTagView.findViewById(R.id.exercise_group);
Button exercise_name = (Button) newTagView.findViewById(R.id.exercise_name);
Button exercise_count = (Button) newTagView.findViewById(R.id.exercise_count);
exercise_group.setText("");
exercise_id.setText("");
exercise_name.setText("No data!");
exercise_calory.setText("");
table_list.removeAllViews();
ex_1_List.addView(newTagView, index);
ex_2_List.addView(newTagView, index); //Line224
ex_3_List.addView(newTagView, index);
ex_4_List.addView(newTagView, index);
}
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/Table2"
android:layout_below="@+id/view1" >
<LinearLayout
android:id="@+id/table_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/add"
android:layout_margin="2dp"
android:orientation="vertical" >
<TableLayout
android:id="@+id/ex_1_List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/blue_btn" />
<TableLayout
android:id="@+id/ex_2s_List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/green_btn" />
<TableLayout
android:id="@+id/ex_3_List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/yellow_btn" />
<TableLayout
android:id="@+id/ex_4_List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/pink_btn" />
</LinearLayout>
</ScrollView>
07-16 01:48:44.503: E/AndroidRuntime(13094): FATAL EXCEPTION: main
07-16 01:48:44.503: E/AndroidRuntime(13094): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.abc/com.example.abc.Exercises_MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.os.Looper.loop(Looper.java:137)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.app.ActivityThread.main(ActivityThread.java:4921)
07-16 01:48:44.503: E/AndroidRuntime(13094): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 01:48:44.503: E/AndroidRuntime(13094): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 01:48:44.503: E/AndroidRuntime(13094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-16 01:48:44.503: E/AndroidRuntime(13094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-16 01:48:44.503: E/AndroidRuntime(13094): at dalvik.system.NativeStart.main(Native Method)
07-16 01:48:44.503: E/AndroidRuntime(13094): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.view.ViewGroup.addViewInner(ViewGroup.java:3620)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.view.ViewGroup.addView(ViewGroup.java:3491)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.widget.TableLayout.addView(TableLayout.java:425)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.view.ViewGroup.addView(ViewGroup.java:3436)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.widget.TableLayout.addView(TableLayout.java:407)
07-16 01:48:44.503: E/AndroidRuntime(13094): at com.examples.abc.Exercises_MainActivity.Inflate_All_Ex_Data(Exercises_MainActivity.java:224)
07-16 01:48:44.503: E/AndroidRuntime(13094): at com. examples.abc.Exercises_MainActivity.onCreate(Exercises_MainActivity.java:144)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.app.Activity.performCreate(Activity.java:5206)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-16 01:48:44.503: E/AndroidRuntime(13094): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-16 01:48:44.503: E/AndroidRuntime(13094): ... 11 more
如何解决这个specified child already has a parent. You must call removeView() on the child's parent first
?
谢谢!
答案 0 :(得分:6)
您正尝试将相同的视图添加到不同的父级。每次要将其添加到其他父级时,必须对同一视图进行充气。
我建议你创建一个for循环,其中有四个限制(你知道你只有四个tableLayouts),每次都会膨胀相同的视图,最后将该视图添加到TableLayout。
for(int i=0;i<4;i++){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newTagView = inflater.inflate(R.layout.exercises_info, null);
...
currentTableLayout.addView(newTagView);
//In this last line you must figure it out how to know what tableLayout corresponds
//to the i index, maybe with the name ¿?
}