Android application stops on button click -
i making application , stops when press buttons
public class menu3 extends themenu{ button buttons[] = new button[21]; intent open = new intent("com.frosti.lidraedi.open"); int clicked; boolean found; int po1; int goon; int s,e; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); actionbar actionbar = getactionbar(); actionbar.setdisplayhomeasupenabled(true); bundle b = getintent().getextras(); po1 = b.getint("po", 0); switch (po1) { case 21: //when press these buttons works setcontentview(r.layout.menu21); s=0; e=11; break; case 22: //the layout loads without error //but when press buttons int layout stops setcontentview(r.layout.menu22); s=12; e=15; break; case 23: setcontentview(r.layout.menu23); s=16; e=20; break; case 24: setcontentview(r.layout.menu24); break; case 25: setcontentview(r.layout.menu25); break; default: break; } init(); } private void init() { for(int c=s; c <= e; c++){ typeface tf = typeface.createfromasset(getassets(),"fonts/sourcesanspro-semibold.otf"); int d = c+1; int viewid = getresources().getidentifier("b"+integer.tostring(d), "id", getpackagename()); if(viewid!=0){ buttons[c] =((button)findviewbyid(viewid)); buttons[c].settypeface(tf); log.d("buttons", integer.tostring(c)+" : "+integer.tostring(d)); }else{ log.d("buttons", "0"); } } } public void buttononclick(view v){ log.d("buttononclick", "i clicked"); found=false; int c= 0; for(button b:buttons){ log.d("for loop", "i got run"); if(b.getid() == v.getid()){ log.d("if block", "i got found"); goon=c; log.d("m3:true:c", integer.tostring(c)+ " : "+integer.tostring(v.getid())); found=true; break; } log.d("c var", "i incremented"); c++; //hehehe log.d("c var", "i got incremented"); } log.i("buttononclick",integer.tostring(c)); if(found) { log.i("m3:clicked:goon",integer.tostring(goon)); bundle b1 = new bundle(); b1.putint("goon",goon); open.putextras(b1); log.d("activity", "i opened"); startactivity(open); overridependingtransition(r.anim.slide_in, r.anim.slide_out); }else if(clicked != v.getid()){ clicked = v.getid(); toast.maketext(getapplicationcontext(),"skjárinn fannst því miður ekki",toast.length_short).show(); } }
ok open application press menu 22(that executes case 22 in switch block) press button , stops these errors get:
if compare image code see executes "log.d("for loop", "i got run");" freaks out, error should in line " if(b.getid() == v.getid()){" have no idea why happens?
xml layout code in case if helps.
i appreciate if me this
try running following loop right after initialize buttons, sure of have:
for(button b : buttons){ log.d("have button id " + b.getid());
then run same loop @ start of onclick(). @ least tell if buttons structure sound. when in doubt of item causing npe, helps log them separately. times, it's non-obvious lifetime error, fragments.
Comments
Post a Comment