gpt4 book ai didi

android - 指定的子项已有父项。您必须先对 child 的 parent 调用 removeView()

转载 作者:行者123 更新时间:2023-11-29 20:15:18 25 4
gpt4 key购买 nike

我想添加一个 textview 和一个 checkboxlinearlayout View :

 for(int i = 0 ; i < helperitems.size() ; ++i){
final UserHelper u = helperitems.get(i);


runOnUiThread(new Runnable() {
@Override
public void run() {

LinearLayout linearLayout = new LinearLayout(ContentChatSendMessage.this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.RIGHT);


ViewGroup.LayoutParams txt_view_params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

CheckBox ch = new CheckBox(ContentChatSendMessage.this);
ch.setId(Integer.parseInt(u.getId()));
linearLayout.addView(ch);

TextView textView = new TextView(ContentChatSendMessage.this);
textView.setLayoutParams(txt_view_params);
textView.setText(u.getUsername1());
linearLayout.addView(ch);

arr_chs.add(ch);
lay_lin_parent.addView(linearLayout);

}
});

}

我的布局:

<LinearLayout
...

<LinearLayout
android:id="@+id/lay_lin_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="4dp">

</LinearLayout>
...
</LinearLayout>

但是我得到这个错误:

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

最佳答案

您的代码尝试将 CheckBox ch 添加到 linearLayout 两次。循环中的第二个 linearLayout.addView() 调用应该是:linearLayout.addView(textView);

关于android - 指定的子项已有父项。您必须先对 child 的 parent 调用 removeView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34036021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com