Error when generating dynamic content

Frisbetarian

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>
ata

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

slugs.map is not a function error when generating dynamic sitemap

Error when summing the content of params dynamic[]

error when generating gridview

Error when generating dynamic Nuxt routes: TypeError: Cannot read property '_normalized' of undefined

Error generating Dynamic Query using ExpressionTree

Android error when generating JSonStringer

JQuery Loading dynamic content error

Form generating error when using GET request

Memory error occurring when generating a list of passwords

Error encountered when generating numbers in series

':app:lintVitalRelease' error when generating signed apk

NuGet - Error when generating nuspec file

Bad request error when generating services

Yo error encode not defined when generating webapp

Ionic, error when generating production build

Attribute error when generating random numbers in Python

etree generating error when using urlib

JMeter error when generating dashboard at end of test

malloc error when generating huge random number

React, error when generating tabs dynamically

Pandas related error when generating heatmap with Bokeh

getting error when generating APK using progurd

crypto.createDecipheriv error when generating iv

Error when trying to generate dynamic table content: Objects are not valid as React child

Webpack dynamic require gives Mixed Content error

javascript dynamic content not affected when ajax call

Polymer: Reload Styles when loading dynamic content

Generating animation on dynamic listview

Generating a dynamic radial background