| Reply | « Previous Thread | Next Thread » |
|
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()); } } } |
|
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 |
|
Quote:
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 } |
|
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 |
|
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); } } |
|
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 |
|
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); } } |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|