I have a simple program with one TextView and two Buttons: Button1 and Button2.
Clicking on Button 1 will start a counter, increasing by 1 every 1 second and show res
Pavel is on the right track with stopping it, except his answer is a waste since it doesn't stop the Timer. Do this on the part he posted with the added else statement:
if(!isPaused)
{
myTextView.setText("count="+count);
count++;
} else {
cancel();//adding this makes it actually stop
}
Oh and you said it crashed. We have no idea why it's crashing, so unfortunately we cannot really help figuring out why its doing that unless we see more code and the error.
Good catch @yorkw The problem is obviously with something its trying to do when its starts running. Since its says "Timer-0", this means it never starts officially and it gets stuck in that part. First rule of running threads, NEVER CHANGE UI ELEMENTS FROM OUTSIDE THE UI. I would suggest using runOnUiThread
like york said. Here's a link on how to use it: runOnUiThread.
Pretty sure TimerTask
is a runnable meaning it runs on a separate thread. Least thats what I noticed on the docs.