android - Why does my score counter always reset? -
i'm trying make game , can't seem score counter work. problem every time button clicked value of integer goes 0.
public void button(view view){ integer counter = integer.valueof(0); counter = counter++; final textview score = (textview) findviewbyid(r.id.score); score.settext("score: " + counter); }
i see why score goes 0 every time don't know how fix it. i'm new development sample code appreciated. thanks.
because resetting it.
integer counter = integer.valueof(0); // here counter = counter++;
use global variable instead.
public class myactivity extends activity{ int counter; protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(...); counter = 0; } public void button(view view){ counter = counter++; final textview score = (textview) findviewbyid(r.id.score); score.settext("score: " + counter); } }
Comments
Post a Comment