You Are Here:

Community: Developer Discussion Boards

#1 Old Main menu - 2004-03-02, 18:34

Join Date: Mar 2003
Posts: 25
Location: Finland
samkri
Offline
Registered User
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;
				
			}
		}
	}
}
Reply With Quote

#2 Old 2004-03-02, 19:18

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
Because destroyApp() and notifyDestroyed() are methods of the MIDlet class, so it should be:
Code:
midlet.destroyApp(true);
midlet.notifyDestroyed();
shmoove
Reply With Quote

#3 Old 2004-03-02, 22:21

Join Date: Mar 2003
Posts: 25
Location: Finland
samkri
Offline
Registered User
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'.
Reply With Quote

#4 Old 2004-03-03, 08:07

Join Date: Jun 2003
Posts: 414
Location: Hungary
kisember
Offline
just a baker
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:
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".
Reply With Quote

#5 Old 2004-03-03, 21:19

Join Date: Mar 2003
Posts: 25
Location: Finland
samkri
Offline
Registered User
So how I have to change the code? :)
Reply With Quote

#6 Old 2004-03-04, 09:40

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
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
Reply With Quote

#7 Old 2004-03-04, 10:11

Join Date: Mar 2003
Posts: 25
Location: Finland
samkri
Offline
Registered User
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;
         }
      }
}
Reply With Quote

#8 Old 2004-03-04, 12:08

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
Yes, and also as Kisember said, adding a select command yourself is unnecessary because an IMPLICIT List will do it for you.

shmoove
Reply With Quote

#9 Old 2004-03-04, 14:04

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

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

#10 Old 2004-03-04, 14:47

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
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
Reply With Quote

#11 Old 2004-03-06, 16:08

Join Date: Mar 2003
Posts: 25
Location: Finland
samkri
Offline
Registered User
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?
Reply With Quote

#12 Old 2004-03-06, 18:10

Join Date: Mar 2003
Posts: 25
Location: Finland
samkri
Offline
Registered User
I solved my problem and now main menu works!
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia