0

I'm trying to generate form elements from a template when the user clicks on a button. I created the template and the container layout for the new forms with XML. Its successfully generating the first form where I'm telling it to generate, but when I try to generate more forms beyond the first one its giving me an error: "the specified child already has a parent. You must call removeView() on the child's first parent". Any clue as to what I should do so as to generate more than one element? I've tried changing the id of the newly created forms but that's giving me a null pointer exception and crashing. Thank you.

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;
        }
    }
}

Added forms should go in "questionsContainer" which is set to be below "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>

1 Answers1

2

You are trying to add the same instance over and over again. Thus the message that "specific child already exists". You would have to create a new template layout instance with different ID (I suppose) and then try to add it in.

So rather than:

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

and adding v1 again. Create a new instance

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

set the id for good measures and then add it again in view.

I suppose you don't need to set the id, but you can read more about how id's work here

Community
  • 1
  • 1
ata
  • 8,853
  • 8
  • 42
  • 68
  • When I created a new instance in Onclick it generated just the first element and stopped without adding anymore, didn't crash and giving me an error though. When I tried to v1.setId(templateID) and then incrementing "templateID" it spawned the form at the top of the layout, losing where its supposed to be created without creating more than one form. – Frisbetarian-Support Palestine Jul 31 '14 at 08:22
  • You are adding in a RelativeLayout. You have to define the position of second item that you add relative to the first item otherwise it will end up on top of first one. Is there any specific need to use relative layout as parent layout or you can use Linear layout as well? Because for linear layout you would just need to add view and it will place it Vertically or Horizontally. – ata Jul 31 '14 at 08:26
  • Thanks! Its correctly adding several ones when the container is a LinearLayout. Only now its adding the forms at the top of the layout regardless of the fact that I've set it to go below a certain element in my page. Any clue? – Frisbetarian-Support Palestine Jul 31 '14 at 08:44
  • You mean that in LinearLayout (as parent), adding new items show them at top of previous one's rather then below? Or is that for RelativeLayout – ata Jul 31 '14 at 08:47
  • Yeah the LinearLayout is the parent/container, when I add its jumping at the top of the entire page, overlaying other layouts, even though the LinearLayout parent container is set to be below another RelativeLayout in my XML. – Frisbetarian-Support Palestine Jul 31 '14 at 08:50
  • i am unsure about your layouts here. Can you update the question and add the solution thats working now (below the question). – ata Jul 31 '14 at 09:03
  • Updated the original question. – Frisbetarian-Support Palestine Jul 31 '14 at 09:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58413/discussion-between-ata-and-frisbetarian). – ata Jul 31 '14 at 09:47