Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User soofika's Avatar
    Join Date
    Nov 2007
    Posts
    42
    Experts...Please take a look at this code. This snippet is the full screen canvas which display an image. I would like to press keycode -7 and go back to the "form" where it came from. The code displays the image OK, but does not want to go back. There are no errors or crashes, just as if keypress event never happens.. Any ideas, what I am doing wrong !!!

    public class iCanvas extends FullCanvas {

    trackGolf tg; // My midlet, calls this full screen canvas
    Image image;

    iCanvas(trackGolf aThis) {

    }

    protected void paint(Graphics g) {
    g.drawImage(image, 0, 0, 0);
    }

    public void createImage(Image s) {
    image = s;
    }
    public void keypressed(int keycode) {
    tg = new trackGolf();
    if (keycode == -7) {
    tg.switchDisplayable(null, tg.getForm());
    }
    }

    }

  2. #2
    Nokia Developer Champion hartti's Avatar
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,192
    What are you doing in the switchDisplayable() method?

    Also if the trackGolf class is extending MIDlet class, you should not try to use new with it...

    Hartti

  3. #3
    Registered User soofika's Avatar
    Join Date
    Nov 2007
    Posts
    42
    Quote Originally Posted by hartti View Post
    What are you doing in the switchDisplayable() method?

    Also if the trackGolf class is extending MIDlet class, you should not try to use new with it...

    Hartti
    Here is the code for switchDisplayable...

    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
    // write pre-switch user code here
    Display display = getDisplay();
    if (alert == null) {
    display.setCurrent(nextDisplayable);
    } else {
    display.setCurrent(alert, nextDisplayable);
    }
    // write post-switch user code here
    }

  4. #4
    Nokia Developer Champion hartti's Avatar
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,192
    Ok, that seems correct, which brings us to my second point.

    You should not try to create another MIDlet in your MIDlet. Without seeing your code it is hard to say what changes you would need to do, but basically you need to either store the reference to your MIDlet somehow for all the classes (it that is really needed) or then you could pass the reference to your MIDLet class as an parameter to all relevant classes.

    Hartti

  5. #5
    Registered User soofika's Avatar
    Join Date
    Nov 2007
    Posts
    42
    I tried following, removing the "new" from trackGolf, but still the same problem.

    public class iCanvas extends FullCanvas {

    Image image;
    Display disp;
    Displayable form;
    trackGolf tg;

    iCanvas(trackGolf aThis) {
    disp = aThis.getDisplay();
    form = aThis.getForm();
    }

    protected void paint(Graphics g) {
    g.drawImage(image, 0, 0, 0);
    }

    public void createImage(Image s) {
    image = s;
    }
    public void keypressed(int keycode) {
    if (keycode == -7) {
    tg.switchDisplayable(null, form);
    }
    }

  6. #6
    Nokia Developer Champion hartti's Avatar
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,192
    Now the variable tg is null in the keypressed method. - Please read my answer above

    Also the method should be keyPressed not keypressed.
    http://developers.sun.com/mobility/midp/articles/event/

    Hartti

  7. #7
    Registered User soofika's Avatar
    Join Date
    Nov 2007
    Posts
    42
    OK Problem solved.. I think I got on the track because of typo that you finally caught keyPressed instead of keypressed. That in combination with the my original midlet was never assigned a value (a null, as you pointed out)... Many thanks for your help... Following code works...

    public class iCanvas extends FullCanvas {

    Image image;
    Display disp;
    Displayable form;
    trackGolf tg;

    iCanvas(trackGolf aThis) {
    disp = aThis.getDisplay();
    form = aThis.getForm();
    tg = aThis;
    }

    protected void paint(Graphics g) {
    g.drawImage(image, 0, 0, 0);
    }

    public void createImage(Image s) {
    image = s;
    }
    public void keyPressed(int keycode) {
    if (keycode == -7) {
    tg.switchDisplayable(null, form);
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved