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)