| Reply | « Previous Thread | Next Thread » |
|
Good evening!
Why my main menu doesn't work? Sun WTK gives the following errors: -cannot resolve symbol : method destroyApp (boolean) -cannot resolve symbol : method notifyDestroyed () Code:
public class Menu extends List implements CommandListener
{
private GameMIDlet midlet; // MIDlet
private Game game; // Game screen
private List list;
private Instructions inst;
private About about;
private Command select = new Command("", Command.ITEM, 1);
private Command exit = new Command("", Command.EXIT, 1);
public Menu(GameMIDlet midlet)
{
super("Game", Choice.IMPLICIT);
this.midlet = midlet;
append("New game", null);
append("Instructions", null);
append("About", null);
append("Exit", null);
addCommand(select);
addCommand(exit);
setCommandListener(this);
}
public void commandAction(Command c, Displayable dl)
{
if (dl == list && c == List.SELECT_COMMAND)
{
switch (list.getSelectedIndex())
{
// Start game
case 0:
game = new Game(midlet, this);
midlet.setDisplayable(game);
break;
// Instructions
case 1:
inst = new Instructions(midlet, this);
midlet.setDisplayable(inst);
break;
// About
case 2:
about = new About(midlet, this);
midlet.setDisplayable(about);
break;
// Exit
case 3:
destroyApp(true);
notifyDestroyed();
break;
}
}
}
}
|
|
Because destroyApp() and notifyDestroyed() are methods of the MIDlet class, so it should be:
Code:
midlet.destroyApp(true); midlet.notifyDestroyed(); |
|
Thanks.
Now main menu appears to the screen but you can't select anything from the list. When pressing 'select' nothing happens and same thing when pressing 'exit'. |
|
Take a look at the first condition in the commandAction method. The list still null so the expression is always false.
Also note (quote from MIDP API javadoc): Quote:
|
|
So how I have to change the code? :)
|
|
Like kisember said, the variable called list is null, you never assigned anything to it (and I seriously doubt that you even need that variable). So that first if statement in commandAction will always be false. Unless the Menu class is a CommandListener for other screens there is no need to test if dl == list, and if there was a need then you would probably want to compare to this and not to list.
shmoove |
|
It must be like this.
Am I right? Code:
public void commandAction(Command c, Displayable dl)
{
if (c == List.SELECT_COMMAND)
{
switch (list.getSelectedIndex())
{
// Start game
case 0:
game = new Game(midlet, this);
midlet.setDisplayable(game);
break;
// Instructions
case 1:
inst = new Instructions(midlet, this);
midlet.setDisplayable(inst);
break;
// About
case 2:
about = new About(midlet, this);
midlet.setDisplayable(about);
break;
// Exit
case 3:
destroyApp(true);
notifyDestroyed();
break;
}
}
}
|
|
Yes, and also as Kisember said, adding a select command yourself is unnecessary because an IMPLICIT List will do it for you.
shmoove |
|
And I don't need to use these lines :
Code:
if (c == List.SELECT_COMMAND)
{
switch (list.getSelectedIndex())
|
|
Comparing to the List.SELECT_COMMAND is probably necessary since you have that exit command there and that's how you would tell the difference between someone choosing a command from the menu and someone pressing the exit command (although you are not handling that exit command, ie, if c == exit).
And in the switch statement what you don't need is that "list" variable (again). Since it's a member method of your class that extends List, you can just do getSelectedIndex() directly. So basically what you'd have is: Code:
public void commandAction(Command c, Displayable dl)
{
if (c == List.SELECT_COMMAND)
{
// an item from the list was chosen,
// to know which one, we do:
switch (getSelectedIndex())
{
// handle list items according to index
}
}
else if (c == exit) {
// the exit command was pressed
// (not the exit item from the list but the
// exit command you added
}
}
|
|
Thank you very much 'shmoove' and 'kisember' but I still have few problems :)
When I'm trying to choose from the menu 'New game', 'Instructions', 'About' or 'Exit' nothing happens. Below is the example: Menu.java : Code:
import javax.microedition.lcdui.*;
public class Menu extends List implements CommandListener
{
private GameMIDlet midlet;
private Game game;
private Command select = new Command("Select", Command.ITEM, 1);
private Command exit = new Command("Exit", Command.EXIT, 1);
public Menu(GameMIDlet midlet)
{
super("Game", Choice.IMPLICIT);
this.midlet = midlet;
append("New game", null);
append("Instructions", null);
append("About", null);
append("Quit", null);
addCommand(exit);
addCommand(select);
setCommandListener(this);
}
public void commandAction(Command c, Displayable dl)
{
if (c == List.SELECT_COMMAND)
{
switch (getSelectedIndex())
{
// Start game
case 0:
game = new Game(midlet, this);
midlet.setDisplayable(game);
break;
// Instructions screen
case 1:
inst = new Instructions(midlet, this);
midlet.setDisplayable(inst);
break;
// About screen
case 2:
about = new About(midlet, this);
midlet.setDisplayable(about);
break;
}
}
else if (c == exit)
{
try
{
midlet.destroyApp(true);
midlet.notifyDestroyed();
}
catch (Exception e)
{
System.out.println("e");
}
}
}
}
Code:
import javax.microedition.lcdui.*;
public class Instructions extends Form implements CommandListener
{
private GameMIDlet midlet;
private Menu menu;
private Command backCommand = new Command("Back", Command.EXIT, 1);
public Instructions(GameMIDlet midlet, Menu menu)
{
super("Instructions");
this.midlet = midlet;
this.menu = menu;
StringItem item = new StringItem(null,
"Instructions...........");
append(item);
addCommand(backCommand);
setCommandListener(this);
}
public void commandAction(Command c, Displayable d)
{
if (c == backCommand)
{
midlet.setDisplayable(menu);
return;
}
}
}
|
|
I solved my problem and now main menu works!
|
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|