×

Discussion Board

Results 1 to 12 of 12

Thread: Main menu

  1. #1
    Registered User
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    25

    Main menu

    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;
    				
    			}
    		}
    	}
    }

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Because destroyApp() and notifyDestroyed() are methods of the MIDlet class, so it should be:
    Code:
    midlet.destroyApp(true);
    midlet.notifyDestroyed();
    shmoove

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    25
    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'.

  4. #4
    Regular Contributor
    Join Date
    Jun 2003
    Location
    Hungary
    Posts
    414
    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):
    IMPLICIT List can be used to construct menus by placing logical commands to elements. In this case no application defined Commands have to be attached. Application just has to register a CommandListener that is called when user "selects".

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    25
    So how I have to change the code?

  6. #6
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    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

  7. #7
    Registered User
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    25
    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;
             }
          }
    }

  8. #8
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Yes, and also as Kisember said, adding a select command yourself is unnecessary because an IMPLICIT List will do it for you.

    shmoove

  9. #9
    Registered User
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    25
    And I don't need to use these lines :

    Code:
     
    if (c == List.SELECT_COMMAND)
    {
          switch (list.getSelectedIndex())
    Am I correct?

  10. #10
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    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
        }
    }
    shmoove

  11. #11
    Registered User
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    25
    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");
           }
        }
      }
    }
    Instructions.java :
    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;
          }
       }
    }
    What is the problem?

  12. #12
    Registered User
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    25
    I solved my problem and now main menu works!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •