gpt4 book ai didi

java - 生成动态内容时出错

转载 作者:行者123 更新时间:2023-12-02 05:35:10 25 4
gpt4 key购买 nike

当用户单击按钮时,我尝试从模板生成表单元素。我使用 XML 创建了新表单的模板和容器布局。它成功生成了我告诉它生成的第一个表单,但是当我尝试生成第一个表单之外的更多表单时,它给了我一个错误:“指定的子项已经有一个父项。您必须在 child 的第一任 parent ”。关于我应该做什么才能生成多个元素的任何线索吗?我尝试更改新创建的表单的 id,但这给了我一个空指针异常并崩溃。谢谢。

public class MakeQuestion extends Activity implements OnClickListener{

private static final int MY_BUTTON = 9000;
int templateID = 1;
Button b;
Button target;
View insertPoint;
Button testTemplate;
View v1;
RelativeLayout.LayoutParams templateParams;
LayoutInflater vi;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.make_question);
Initialize();
}

public void Initialize(){
//button for adding new forms
b = (Button) findViewById(R.id.makeLayoutButton);
b.setOnClickListener(this);

//get the template form to be duplicated
vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v1 = vi.inflate(R.layout.form_template, null);

//set the params for the element that will have dynamically generated content below it
templateParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

//container where forms will be contained in
insertPoint = findViewById(R.id.questionsContainer);

//view where new form will go below
View belowContainer = findViewById(R.id.questionTemplateFake);

//set id for layout params of view
belowContainer.setId(1);

//set rule for new forms to go below view 'belowContainer'
templateParams.addRule(RelativeLayout.BELOW, belowContainer.getId());

}

public void onClick(View v) {
switch (v.getId()) {
case R.id.makeLayoutButton:
v1 = vi.inflate(R.layout.form_template, null);

//add view to the insertPoint
((LinearLayout) insertPoint).addView(v1);

break;
}
}
}

添加的表单应放入“questionsContainer”中,该容器设置为“questionTemplateFake”下方

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/grey_background" >

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#8B459A"
android:baselineAligned="false"
android:clipToPadding="false" >

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/logo_small" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:src="@drawable/settings" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="@drawable/search" />
</RelativeLayout>

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/relativeLayout1" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/relative12"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="WHAT IS YOUR QUESTION?" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/save_button"
android:layout_alignBottom="@+id/save_button"
android:layout_alignRight="@+id/textView1"
android:text="ADD PICTURE OR VIDEO"
android:textSize="10sp" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:background="@drawable/textlines"
android:ems="10"
android:hint="50 WORDS OR LESS"
android:inputType="textMultiLine"
android:paddingLeft="5dp" />

<Button
android:id="@+id/save_button"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="16dp"
android:background="@drawable/purplebutton"
android:text="BROWSE"
android:textColor="@drawable/button_text_color"
android:textSize="10sp" />

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/save_button"
android:layout_marginTop="25dp"
android:text="CREATE AN ANSWER" />



<RelativeLayout
android:id="@+id/questionTemplateFake"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextView01" >

<Button
android:id="@+id/Button02eew"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/purplebutton"
android:text="BROWSE"
android:textColor="@drawable/button_text_color"
android:textSize="10sp" />

<EditText
android:id="@+id/EditText02"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_above="@+id/Button02"
android:layout_alignParentLeft="true"
android:background="@drawable/textlines"
android:ems="10"
android:hint="50 WORDS OR LESS"
android:inputType="textMultiLine"
android:paddingLeft="5dp" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="19dp"
android:layout_toLeftOf="@+id/Button02"
android:text="ADD PICTURE OR VIDEO"
android:textSize="10sp" />

</RelativeLayout>

<LinearLayout
android:id="@+id/questionsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:layout_below="@+id/questionTemplateFake"
>
</LinearLayout>

</RelativeLayout>

</LinearLayout>
</ScrollView>

<Button
android:id="@+id/makeLayoutButton"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/scrollView1"
android:layout_marginRight="17dp"
android:layout_marginTop="79dp"
android:background="@drawable/purplebutton"
android:text="MORE OPTIONS"
android:textColor="@drawable/button_text_color"
android:textSize="10sp" />

</RelativeLayout>

最佳答案

您正在尝试一遍又一遍地添加相同的实例。因此,消息“特定的 child 已经存在”。您必须创建一个具有不同 ID 的新模板布局实例(我想),然后尝试将其添加进去。

所以而不是:

((RelativeLayout) insertPoint).addView(v1, templateParams);

并再次添加 v1。创建一个新实例

v1 = vi.inflate(R.layout.form_template, null);

设置好措施的 ID,然后在 View 中再次添加它。

我想你不需要设置id,但是你可以阅读更多关于如何设置的信息id's work here

关于java - 生成动态内容时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25053742/

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