java - My code receives the error "The specified child already has a parent. You must call removeView() on the child's parent first." -
as title reads, getting error in logcat when run app. code written after error occured:
public void coinanim1() { relativelayout rl = (relativelayout) findviewbyid(r.id.mainlayoutid); imageview coin1 = new imageview(this); relativelayout.layoutparams params = new relativelayout.layoutparams(30, 40); params.leftmargin = 50; coin1 = (imageview) findviewbyid(r.id.coinid); animation coinfall1 = animationutils.loadanimation(this, r.anim.coinanimation); coin1.startanimation(coinfall1); rl.addview(coin1, params); } public void coinanim2() { relativelayout rl2 = (relativelayout) findviewbyid(r.id.mainlayoutid); imageview coin2 = new imageview(this); relativelayout.layoutparams params2 = new relativelayout.layoutparams(30, 40); params2.leftmargin = 50; coin2 = (imageview) findviewbyid(r.id.coin2id); animation coinfall1 = animationutils.loadanimation(this, r.anim.coinanimation); coin2.startanimation(coinfall1); rl2.addview(coin2, params2); }
logcat says line "rl.addview(coin1, params);" causes error.does know can fix this? there similar questions, new coding don't know how adapt answers problem.
any appreciated.
problem solved:
ashishduh solved problem replacing line:
rl.addview(coin1, params);
with line:
coin1.setlayoutparams(params);
hope helps someone! ashi <3
the view coin1
has parent (the relativelayout inflated). can't assign parent without removing relativelayout.
i think you're trying modify layoutparams of coin1. instead of doing this: rl.addview(coin1, params);
need set layoutparams coin1.setlayoutparams(params);
Comments
Post a Comment