Android开发中,请问当在一个视图中addView另一个布局视图时为什么报错?

Android开发中,我在一个视图中addView另一个布局视图(该视图通过inflate加载获得,其中root为null即没有附加parent视图),为什么还是会报错误:

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

相关代码如下:

fragment.class:

public class ScheduleFragment extends BaseFragment implements ScheduleFragmentI{
    private View view;
    private View upDownItem;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_schedule,null);
        upDownItem=inflater.inflate(R.layout.item_up_down,null);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        scheduleFragmentPI=new ScheduleFragmentP(this);
        timeList=new ArrayList<>();
        timeList=scheduleFragmentPI.getClassTime(getContext());
        LinearLayout weekLinear=view.findViewById(R.id.linear_week);
        TextView upTextView=upDownItem.findViewById(R.id.textView_up);
        TextView downTextView=upDownItem.findViewById(R.id.textView_down);
        for(int i=0;i<10;i++){
            upTextView.setText(String.valueOf(i+1));
            downTextView.setText(timeList.get(i));
            weekLinear.addView(upDownItem);
        }
    }
}

就是weekLinear.addView(upDownItem);这行代码报错!

fragment_schedule.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:id="@+id/linear_week"
        android:layout_width="0dp"
        android:layout_height="40dp"
        app:layout_constraintStart_toEndOf="@id/textView_month"
        app:layout_constraintEnd_toEndOf="parent"
        android:orientation="horizontal"/>

</android.support.constraint.ConstraintLayout>

item_up_down.xml:

<?xml version="1.0" encoding="utf-8"?>
<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: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>

错误如下:

    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)
阅读 3.4k
3 个回答
LayoutInflater.inflate()出来的视图,addView(View)1次是可以的;
但是add 10次又不重新inflate,那可是不行的。

动态添加的子 View 要指定 LayoutPararms ,且子 View 的 LayoutParams 的类型要和父 View 的 LayoutParams 相同。

文章中的问题是

for(int i=0;i<10;i++){
    upTextView.setText(String.valueOf(i+1));
    downTextView.setText(timeList.get(i));
    // 这里同一个 upDownItem 被添加了多次,这是不行的
    weekLinear.addView(upDownItem); 
}

为什么没有指定layoutparams?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏