[J2ME] Timer/TimerTask Question...

Started by
4 comments, last by _nomad_ 18 years, 8 months ago
Hello, My Timer seems to always stop whenever I call a display.setCurrent(something);, where display is of type Display. Here's how the code looks like (modified for brevity):
public void startApp() {
        t = new TextBox("ABC", " ", 128, TextField.ANY);

        t.addCommand(loginCommand);
        t.addCommand(exitCommand);
        t.setCommandListener(this);

        display.setCurrent(t);

        iTimer = new Timer();
        iTimer.scheduleAtFixedRate(taskUpdateMsg, 1000, 5000);
  ...
  ...
  ...
}
startApp() created my iTimer, and it works fine. I System.out.println() inside taskUpdateMsg's run() method and it does show up as scheduled. And then I do this:
public void commandAction(Command c, Displayable s) { 
  if (c == loginCommand) {
    myList = new List("My List", List.IMPLICIT);

    myList.addCommand(selectUserCommand);
    myList.addCommand(logoutCommand);
    myList.setCommandListener(this);

    display.setCurrent(myList);
  }

  ...
  ...
  ...
}
when it reaches display.setCurrent(myList), the timer stops...but when I try to create it again, I get an error that says something like it's been created or something exception. Why is this occuring whenever I call Display.setCurrent()? Thanks.
Advertisement
It shouldn't be stopping.

But what exactly are you doing in the TimerTask's run() method? Maybe changing the current Displayable affects it's execution and somehow makes it throw an Exception or something?

shmoove
I also had the same problem, nothing unusual in run method..

actually I was calling display.setCurrent(new MyGameCanvas());

and the timers was only called once..

now I do:

MyGameCanvas c=new MyGameCanvas();
display.setCurrent(c);
c.init();
nope, nothing in the run() method can cause it to throw an exception...:(

I'll look into your solution/suggestion Marcel.

is this a bug in j2me?

thanks.
don´t know if this is bug in j2me, this happened in the emulator, but I can´t remember if I also tested in the cell phone also.

I didn´t waste much time with this, I found a work around and it´s fine by me
well that's mighty odd...i used kxml and modified/fixed its code (its binary xml implementation is BUGGY!), and this timertask problem does not happen anymore...

i never got exception, etc. before or even now (i also have one in the timertask run() method)...the fix on kxml had nothing to do with anything outside of kxml (other than the text it spews out when i call getText())...

hmm....

This topic is closed to new replies.

Advertisement