Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Java illegal state Exception when trying to add View by inflating xml
    text
    copied!<p>Em getting java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.</p> <p>Not able to find out which view has to be removed, any help would be really helpful.</p> <p>Here is the Code snippet </p> <p>main.xml </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:id="@+id/mainLayout" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; </code></pre> <p>data.xml </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Activity Code </p> <pre><code>public class ToDo extends Activity { /** Called when the activity is first created. */ Button addNew; RelativeLayout mainLayout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mainLayout=(RelativeLayout)findViewById(R.id.mainLayout); RelativeLayout rel; LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); for(int idx=0;idx&lt;2;idx++){ RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rel = (RelativeLayout) inflater.inflate(R.layout.data,null); params.setMargins(0, 50, 0, 0); TextView fromWeb= (TextView) rel.findViewById(R.id.txt); fromWeb.setText("AA"); mainLayout.addView(rel,params); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload