title: Adding view causes "Child already has parent" error
body: <p>I'm making a gallery app and adding image buttons dynamically using the code below, this works on one of my devices but on the other I get a</p>
  
  <pre><code>  java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.  
   Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  E/AndroidRuntime( 3358):        at android.view.ViewGroup.addViewInner(ViewGroup.java:3381)
  E/AndroidRuntime( 3358):        at android.view.ViewGroup.addView(ViewGroup.java:3252)
  E/AndroidRuntime( 3358):        at android.view.ViewGroup.addView(ViewGroup.java:3197)
  E/AndroidRuntime( 3358):        at android.view.ViewGroup.addView(ViewGroup.java:3173)
  E/AndroidRuntime( 3358):        at package.MainActivity.onCreate(MainActivity.java:74)
  </code></pre>
  
  <p>error, but on the other device no problems at all and it runs fine. The one that it works on is running 4.1.2 and the one it crashes on is running 4.1.1 could this be it? the project's min and targeted sdk is 16  </p>
  
  <pre><code>   setContentView(R.layout.activity_main);
  
      // Hook up clicks on the thumbnail views.
  
  
      imgHolder = (LinearLayout)findViewById(R.id.imgHolder);
  
      for(int x = 0; x &lt; files.length; x++)
      {
          Log.d("Conor",files[x].getAbsolutePath());
          Drawable img = Drawable.createFromPath(files[x].getAbsolutePath());
          imgs.add(img);
          thumbs.add(new ImageButtons(this, files[x].getAbsolutePath(),x));
          thumbs.get(x).setImageDrawable(img);
          thumbs.get(x).setOnClickListener(onCl);
          imgHolder.addView(thumbs.get(x));
      }
      // Retrieve and cache the system's default "short" animation time.
      mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
  </code></pre>
  
  <p>ImageButtons class that <code>thumbs</code> is an `ArrayList of </p>
  
  <pre><code>import java.io.IOException;
  
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  
  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Color;
  import android.graphics.drawable.Drawable;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.util.Xml;
  import android.view.ViewGroup.LayoutParams;
  import android.widget.ImageButton;
  
  public class ImageButtons extends ImageButton{
  
  
      Resources res = getResources();
  
  
      public ImageButtons(Context context,String img,int id) 
      {
          super(context);
          XmlPullParser parser = res.getXml(R.layout.imagebuttons);
          AttributeSet attributes = null;
          int state = 0;
          while(state != XmlPullParser.END_DOCUMENT)
          {
              try {
                  state = parser.next();
              } catch (XmlPullParserException e1) {
                  e1.printStackTrace();
              } catch (IOException e1) {
                  e1.printStackTrace();
              }       
              if (state == XmlPullParser.START_TAG) {
                  if (parser.getName().equals("ImageButton")) {
                     attributes = Xml.asAttributeSet(parser);
                      break;
                  }
              }
          } 
  
          setId(id);
  
          setLayoutParams(new LayoutParams(context, attributes));
      }
  
  
  }
  </code></pre>
  
  <p>thumbs is an <code>ArrayList&lt;ImageButton&gt;</code>
  and line 74 refers to `imgHolder.addView(thumbs.get(x));</p>
  
  <p>Again my question is why this works on some devices but not others </p>