Blackberry timer not firing TimerTask
I am trying to use a time that updates a label every second (so it shows a countdown) but it only appears to be "ticking" once and I can't work out what I'm doing wrong!
   public class Puzzle extends UiApplication {
public static void main(String[] args) {
    Puzzle puzzle = new Puzzle();
    puzzle.enterEventDispatcher();
}
public Puzzle() {
     pushScreen(new PuzzleScreen());
}
}
class PuzzleScreen extends MainScreen {
LabelField timerLabel;
Timer timer;
public static int COUNT = 0;
public PuzzleScreen() {
    //set up puzzle
    VerticalFieldManager vfm = new VerticalFieldManager();
    add(vfm);
    timerLabel = new LabelField();
    timerLabel.setText("00:20");
    vfm.add(timerLabel);
    StartTimer();
}
void StartTimer() {
    timer = new Timer();
    timer.schedule(new TimerTick(), 1000);
}
private class TimerTick extends TimerTask {
    public void run() {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                timerLabel.setText((COUNT++) + "");
            }
        });
    }
}
Can anyone see what I am doing wrong..? All that happens is my label get's set to "0" and then开发者_如何学JAVA doesn't change. I have put a breakpoint on the run in the timer tick class but I don't see it firing!
Bex
You'll need to change your Timer's schedule() call to
timer.schedule(new TimerTick(), 0, 1000);
The way you're calling it right now is saying to run it once after a second delay. This way says to run it now and every second. You probably want to use
timer.scheduleAtFixedRate(new TimerTick(), 0, 1000); 
though, because it will make sure that on average your TimerTask is ran every second rather than with a normal schedule() call that says it will try waiting a second then executing, but it could fall behind if something slows it down. If scheduleAtFixedRate() is delayed, it will make multiple calls quicker than on the 1 second delay so it can "catch up." Take a look at http://www.blackberry.com/developers/docs/5.0.0api/java/util/Timer.html#scheduleAtFixedRate(java.util.TimerTask,%20long,%20long) for a more detailed explanation.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论