android 布局右边视图没了,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:

android:layout_width="match_parent"

android:layout_height="match_parent"

xmlns:app="http://schemas.android.com/apk/res-auto">

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"/>

item_up_down.xml:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/textView_up"

android:layout_width="wrap_content"

android:layout_height="0dp"

android:layout_weight="1"

android:gravity="center"/>

android:id="@+id/textView_down"

android:layout_width="wrap_content"

android:layout_height="0dp"

android:layout_weight="1"

android:gravity="center"/>

错误如下:

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值