0

What i am doing:: I am adding dynamically a xml to a container

What is happening::

  • I am able to add the view for the first tme on first onclick
  • but second time it gives the error as shown in log
  • I guess i cannot add without assigning ids
  • How can i dynamically add ids for all teh views inside on everytime onclick

MainActivity.java

public class MainActivity extends Activity {


    LinearLayout dynAddrId,rootContainer;
    ImageView btnAddId,btnRemoveId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dynAddrId=(LinearLayout) findViewById(R.id.dynAddrId);
        btnAddId=(ImageView) findViewById(R.id.btnAddId);
        btnRemoveId=(ImageView) findViewById(R.id.btnRemoveId);
        rootContainer=(LinearLayout) findViewById(R.id.rootContainer);

        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View addView = layoutInflater.inflate(R.layout.row, null);


        btnAddId.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                        dynAddrId.addView(addView);
            }
        });

    }
}

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:orientation="horizontal" >

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:entries="@array/languagesList" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/rootContainer"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:orientation="horizontal" >

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:entries="@array/languagesList" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />

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

            <ImageView
                android:id="@+id/btnAddId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />

            <ImageView
                android:id="@+id/btnRemoveId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/dynAddrId" >
    </LinearLayout>

</LinearLayout>

Log::

12-10 11:24:16.767: E/AndroidRuntime(14953): FATAL EXCEPTION: main
12-10 11:24:16.767: E/AndroidRuntime(14953): Process: com.example.addaddressproof, PID: 14953
12-10 11:24:16.767: E/AndroidRuntime(14953): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3564)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.view.ViewGroup.addView(ViewGroup.java:3417)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.view.ViewGroup.addView(ViewGroup.java:3362)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.view.ViewGroup.addView(ViewGroup.java:3338)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at com.example.addaddressproof.MainActivity$1.onClick(MainActivity.java:43)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.view.View.performClick(View.java:4456)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.view.View$PerformClick.run(View.java:18465)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.os.Handler.handleCallback(Handler.java:733)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.os.Looper.loop(Looper.java:136)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at android.app.ActivityThread.main(ActivityThread.java:5086)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at java.lang.reflect.Method.invoke(Method.java:515)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-10 11:24:16.767: E/AndroidRuntime(14953):    at dalvik.system.NativeStart.main(Native Method)
3
  • move View addView = layoutInflater.inflate(R.layout.row, null); under ButtonClick(....) Commented Dec 10, 2014 at 6:04
  • Try to move addView initialization code in on click. Commented Dec 10, 2014 at 6:04
  • In addition to the above comments, I would mention that the Activity class offers the getLayoutInflater() method. Commented Dec 10, 2014 at 6:08

2 Answers 2

3

Try to addView code inside button onClick :

btnAddId.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
       View addView = LayoutInflater.from(MainActivity.this).inflate(R.layout.row,null);
       dynAddrId.addView(addView);
    }
});

Note : Try use ScrollView as parent for dynAddrId LinearLayout so which handle views overflow on screen.

Sign up to request clarification or add additional context in comments.

Comments

0

The specified child already has a parent.

The error means that the view you are trying to show on the screen is already on the screen.

In your case, you are creating and storing the view addView. When you click the button for the first time, it gets shown on the screen. When you click the next time, you are bound to get the error.

As suggested in the comments, move the initialization to onClick().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.