0

Why does the timer stop working when i put the postDelayed(this,1000); inside the if statement, just under seconds++;? There are 3 buttons (start,stop,reset) in the layout. Press Start->running=true,press stop->running=stop,press reset->running=false seconds=0

private void runTimer() {

    final TextView timeView = (TextView) findViewById(R.id.time_view);
    final Handler handler = new Handler();

    handler.post(new Runnable() {

        @Override
        public void run() {
            int hours = seconds / 3600;
            int minutes = (seconds % 3600) / 60;
            int secs = seconds % 60;

            String time = String.format("%d:%2d:%02d", hours, minutes, secs);
            timeView.setText(time);
            if (running) {
                seconds++;
            //handler.postDelayed(this, 1000);
            //doesnt work if i put it here
            }
            handler.postDelayed(this, 1000);
        }
    });

}
  • please add your code here. – ADyson Nov 22 '17 at 22:32
  • It doesnt allow me to do that..there is error i dont know why – Allicio Black Nov 22 '17 at 22:37
  • It's definitely possible to add code to your question. Don't paste all your code, just the parts necessary to reproduce the issue. There is guidance in the SO help pages if you're having trouble using the question editor. If you mention what the error was as well, perhaps we can assist. – ADyson Nov 22 '17 at 22:39
  • 1
    Also I suggest you to read the post before to send it. Possibly you want to improve the format, add the capital letters after the comma, and insert your code statements between two ` (They are called backquote) – trocchietto Nov 22 '17 at 22:43
  • What is wrong with my post? Why do you all want to block my post wth – Allicio Black Nov 24 '17 at 00:41
  • Im not asking any stupid question or whatever – Allicio Black Nov 24 '17 at 00:43

1 Answers1

0

At the time you call runTimer() the variable running is set to false. Most likely you have to move the call to runTimer() into the onClickStart() method (after setting running to true).
When declaring booleans their default value is assumed with false.

Ch4t4r
  • 1,387
  • 1
  • 11
  • 30
  • When i put postdelayed just outside the if statement,it works.why? – Allicio Black Nov 24 '17 at 00:45
  • @AllicioBlack it works as expected because it is outside from the imprisonment of the `false` `if-else statement block`. The issue of your code is your `running` variable which always false, I cannot totally answer it since it is not the entire code. – Enzokie Nov 24 '17 at 06:02
  • There are 3 buttons (start,stop,reset) in the layout. Press Start->running=true,press stop->running=stop,press reset->running=false seconds=0 – Allicio Black Nov 24 '17 at 06:31
  • U all said that in default running is false.but if i press start.it should set running as true.and it should increment the seconds and pause for 1sec.no?(postdelayed inside if else statement) – Allicio Black Nov 24 '17 at 06:38
  • Yes, running is set to true. But at that point the runnable isn't running anymore because it is only repeated if `running` is true. The flow is like this: `runTimer` is called => the runnable is started => it does the work => it checks whether running is true => running is false and thus the runnable is not re-run in 1 second => done. If you want the runnable to run when the start button is clicked post it in the click method (if running was false before) Again, you don't seem to really get the grasp of it, I suggest you learn java first. Correct me if I'm wrong on that. – Ch4t4r Nov 24 '17 at 10:00