问题描述:

I have a form that gives the user the option to add more fields to fill. The user adds more fields by clicking on a button. When the button is clicked It duplicates a RelativeLayout that houses form elements in it inside of another RelativeLayout.

This is the form that I'm trying to duplicate

<RelativeLayout

android1:id="@+id/questionTemplate"

android1:layout_width="wrap_content"

android1:layout_height="60dp"

android1:layout_alignParentLeft="true"

android1:layout_below="@+id/TextView01"

>

<Button

android1:id="@+id/Button02"

android1:layout_width="75dp"

android1:layout_height="20dp"

android1:layout_alignParentBottom="true"

android1:layout_alignParentRight="true"

android1:background="@drawable/purplebutton"

android1:text="BROWSE"

android1:textColor="@drawable/button_text_color"

android1:textSize="10sp"

/>

<EditText

android1:id="@+id/EditText02"

android1:layout_width="match_parent"

android1:layout_height="25dp"

android1:layout_above="@+id/Button02"

android1:layout_alignParentLeft="true"

android1:background="@drawable/textlines"

android1:ems="10"

android1:hint="50 WORDS OR LESS"

android1:inputType="textMultiLine"

android1:paddingLeft="5dp"

>

<requestFocus />

</EditText>

<TextView

android1:id="@+id/TextView03"

android1:layout_width="wrap_content"

android1:layout_height="wrap_content"

android1:layout_alignParentBottom="true"

android1:layout_marginRight="19dp"

android1:layout_toLeftOf="@+id/Button02"

android1:text="ADD PICTURE OR VIDEO"

android1:textSize="10sp"

/>

</RelativeLayout>

Its inside another RelativeLayout with an id of "questionsContainer". And when the user clicks on a button with an id of "makeLayoutButton" its supposed to add another instance of "questionTemplate" below another one.

Here's what I'm doing when the user clicks on the button

public void onClick(View v) {

Toast toast;

switch (v.getId()) {

case R.id.makeLayoutButton:

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

// template to be copied

RelativeLayout target = (RelativeLayout) v1.findViewById(R.id.questionTemplate);

// insert into main view

RelativeLayout insertPoint = (RelativeLayout)findViewById(R.id.questionsContainer);

insertPoint.addView(target);

break;

}

}

But it keeps crashing and the log informs me that I need to call removeView on the parent first. But when I do that it just removes all the children of the container and leaves me with one copy of the form without it duplicating. Any clues as to where I'm going wrong? I have a feeling it has something to do ids.

网友答案:

Of course, the output is correct. You're trying to add a View which already exists. You don't create (duplicate) new View but use the existing one by using findViewById() method.

In order to create the actual View, you should use inflate() method:

RelativeLayout target = (RelativeLayout) v1.inflate(R.id.questionTemplate, null); // if v1 is container for this R.id.questionTemplate layout
相关阅读:
Top