Android: boolean value not changing? -


in program, boolean value (called metronomeon) supposed set true @ click of 1 button, , false @ click of different button. class have (mainactivity) implements runnable. after thread gets started, in run method, depending on value of metronomeon, it's supposed execute block of code. problem after onclick method button supposed set metronomeon true, metronomeon's value isn't changed.

buttonlisteners (the method called in oncreate):

public void buttonlisteners() {     bstart.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {             metronomeon = true;         }     });      bstop.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {             currentbeatindex = 1;             tvcurrentbeatindex.settext("");             metronomeon = false;         }     }); } 

the run method:

if (metronomeon) {         tvbpm.settext("in if statement");         //other code...     } 

i have absolutely no idea why metronomeon isn't switching values. know run method gets executed, , metronomeon ever changed in oncreate , ondestroy. ideas why isn't working? in advance!!

edit**************************** i've gotten rid of run method , implementation of runnable class, there no problem there, yet metronomeon still returns false.

edit again************************ have no idea why or how, after deleting runnable stuff , restarting eclipse, works. metronomeon changes value! thank gave advice!

multiple possible reasons:

  • is metronomeon volatile? if variable used across threads, should mark it.
  • does run method check metronomeon multiple times? code posted, checked 1 time
  • is run method executed think is? if , metronomeon set, activity crash, allowed access gui ui thread. making class implement runnable not cause thread spawn. , can call run method directly without thread.

from little parts see of application, think want use services instead of threads/runnables? services can started , stopped, run independently of activity , may spawn threads.

as cannot access activity service, may debug output android logging methods in log class.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -