Android: find the id of the inflated buttons -
i have inflated buttons using following codes, , swap location of 2 buttons selected using simple translateanimation
.
codes:
(int k = 1; k <= quotient; k++) { linearlayout.layoutparams params3 = new linearlayout.layoutparams(button_width,row_height); params3.setmargins(button_margin, button_margin, button_margin, button_margin); btn_right = new button(this); btn_right.setid(idd); final int id_ = btn_right.getid(); btn_right.settext(""+question[idd]); frame_row.addview(btn_right, params3); btn_right.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { btn_right = ((button) findviewbyid(id_)); x1 = getrelativeleft(btn_right); y1 = getrelativetop(btn_right); int b = integer.parseint(""+btn_right.gettext().tostring()); selection ++; if (selection%2 ==1) { selected_id1 = id_; selected_color1 = b; custom_toast("x1=" +x1 + "\ny1=" + y1); } else { selected_id2 = id_; selected_color2 = b; x2 = getrelativeleft(btn_right); y2 = getrelativetop(btn_right); custom_toast("x2=" +x2 + "\ny2=" + y2); // first 1 btn_right = ((button) findviewbyid(selected_id1)); anim1=new translateanimation(0, (x2-x1), 0, (y2-y1)); anim1.setfillafter(true); anim1.setduration(animationtime); anim1.setanimationlistener(game.this); btn_right.startanimation(anim1); // second 1 btn_right = ((button) findviewbyid(selected_id2)); anim2=new translateanimation(0, (x1-x2), 0, (y1-y2)); anim2.setfillafter(true); anim2.setduration(animationtime); anim2.setanimationlistener(game.this); btn_right.startanimation(anim2); } @override public void onanimationend(animation animation) { if (animation == anim1) { custom_toast("1 clear"); btn_right = ((button) findviewbyid(selected_id1)); btn_right.clearanimation(); // line 426 } if (animation == anim2) { custom_toast("2 clear"); btn_right = ((button) findviewbyid(selected_id2)); btn_right.clearanimation(); } }
logcat:
04-21 21:28:00.728: e/androidruntime(11341): java.lang.nullpointerexception 04-21 21:28:00.728: e/androidruntime(11341): @ com.abc.abc.game.onanimationend(game.java:426)
question:
for info, if selection%2 ==1, means user has pressed first button , not yet ready swap, , hence record id , x, y coordinates. if selection%2 ==0, means user has pressed 2 buttons , ready swap after recording 2nd's x , y.
also, (x1, y1) , (x2, y2) coordinates reporting correctly.
it runs npe @ line 426 btn_right.clearanimation();
(as specified above) . question is, since buttons inflated, , have setid inflated buttons, why still runs out npe @ line 426, or how should specify 2 buttons user has touched implement swapping animation?
thanks!!
create list of buttons, can create buttons , set id, tags , onclicklistenners , add them button list:
buttonlist = new arraylist<button>(); (int i=0;i<2;i++){ button button = new button(getapplicationcontext()); button.setonclicklistener(customlistenner); button.setanimation(anim); button.setid(i); button.settag(i); mylayout.addview(button); buttonlist.add(button); }
and when need use button again, call id or tags list. (buttonlist.get(0).getid()
)
if need different listenners, can control them using unique tag check in if function , declare action.
this method use when need create , use animations on dynamic views programmatically.
Comments
Post a Comment