In Android development, why did you report an error when you addView another layout view in one view?

in Android development, I addView another layout view in one view (this view is obtained through inflate loading, where root is null, that is, there is no attached parent view). Why do I still report an error:

The specified child already has a parent. You must call removeView () on the child"s parent first.

the related code is as follows:

fragment.class:



    <TextView
        android:id="@+id/textView_up"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"/>

    <TextView
        android:id="@+id/textView_down"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"/>

</LinearLayout>

the error is as follows:

    java.lang.RuntimeException: Unable to start activity ComponentInfo.classschedule.view.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child"s parent first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7425)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Jun.03,2022

LayoutInflater.inflate () comes out of the view, addView (View) once is OK;
but add 10 times without re-inflate, that is not possible.

the dynamically added child View should specify LayoutPararms, and the LayoutParams type of the child View should be the same as the LayoutParams of the parent View.

the problem in the article is

  

Why is layoutparams not specified?

Menu