android - Why not I can execute ".startAnimation" in a "for" loop for several buttons? -
i have grid 12 buttons , want animate buttons. have them in vector , when intend execute ".startanimation" have "nullpointer" exception , don't know why.
i have this:
final private int tamgrid=12; private button[] botones = new button[tamgrid]; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_juego_grid12); loopanimation=animationutils.loadanimation(this, r.anim.animacionbotongrid12); //asociamos los elementos de la vista: asociateelements(); } public void asociateelements(){ string buttonid; int resid; for(int i=0; i<tamgrid; i++) { buttonid="boton"+integer.tostring(i); resid = getresources().getidentifier(buttonid, "id","butterflydevs.brainstudio"); buttons[i]=(button)findviewbyid(resid); buttons[i].startanimation(loopanimation); } } why don't work this? error is
caused by: java.lang.nullpointerexception in line of startanimation.
i've noticed happens when try this:
botones[0].setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { //acciones del botón: botones[0].setbackgroundcolor(color.red); } } ); when use index in "botones[n]". has happened anyone?
i suggest put loop inside onstart() callback. maybe buttons have not yet been created @ point in code.
Comments
Post a Comment