Sign in to follow this  
_nomad_

[J2ME] Timer/TimerTask Question...

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this