java - Adding view causes "Child already has parent" error -


i'm making gallery app , adding image buttons dynamically using code below, works on 1 of devices on other a

  java.lang.runtimeexception: unable start activity componentinfo{package/package.mainactivity}: java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first.    caused by: java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first. e/androidruntime( 3358):        @ android.view.viewgroup.addviewinner(viewgroup.java:3381) e/androidruntime( 3358):        @ android.view.viewgroup.addview(viewgroup.java:3252) e/androidruntime( 3358):        @ android.view.viewgroup.addview(viewgroup.java:3197) e/androidruntime( 3358):        @ android.view.viewgroup.addview(viewgroup.java:3173) e/androidruntime( 3358):        @ package.mainactivity.oncreate(mainactivity.java:74) 

error, on other device no problems @ , runs fine. 1 works on running 4.1.2 , 1 crashes on running 4.1.1 it? project's min , targeted sdk 16

   setcontentview(r.layout.activity_main);      // hook clicks on thumbnail views.       imgholder = (linearlayout)findviewbyid(r.id.imgholder);      for(int x = 0; x < 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 , cache system's default "short" animation time.     mshortanimationduration = getresources().getinteger(android.r.integer.config_shortanimtime); 

imagebuttons class thumbs `arraylist of

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

thumbs arraylist<imagebutton> , line 74 refers `imgholder.addview(thumbs.get(x));

again question why works on devices not others

you might need handling:

view v = thumbs.get(x); viewgroup p = (viewgroup)v.getparent(); p.removeview(v); imgholder.addview(v); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -