Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

How can I create a MenuItem

Hi,

 

I'm trying to create a MenuItem in my application.

 

I have  tried many ways, some shows me how to create a MenuItem in the build-in apps, some don't work - trows me an exception.

 

 

I'm looking for a straight way to create a simple MenuItem in my application.

 

Thanks,

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How can I create a MenuItem

the most simple way would be to use http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/container/MainScreen.html#a...
for example in the constructor of your mainscreen.

to have it more orderly you can overwrite http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/container/MainScreen.html#m... int) and use menu.add to add own menu items.

I have also retrieved an old, but still valid and very helpful article from my KB, i hope it'll help you understand menu items better:
http://www.slashdev.ca/2008/05/07/menuitem-demystified/
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: How can I create a MenuItem

Hi, Thank you for your reply.

 

I tried to create a MenuItem -

addMenuItem(new MenuItem(new StringProvider("Send by Email"), 0, 0));

 

But when I launch the application I receive a RuntimeException.

 

Thanks again...

Contributor
Posts: 34
Registered: ‎10-05-2010
My Device: simulator 9550

Re: How can I create a MenuItem

Hello

 

Have you tried define the class MenuItem example:

 

 

public class menuItemExit extends MenuItem
{

 public menuItemExit(String arg0,int arg1,int arg2){
  super(arg0, arg1, arg2);
 }

 public void run() {
  System.exit(0);
 }
 
 
}

 

And then

 

_menuItem= new menuItemExit("Send by Email",0,0);

addMenuItem(_menuItem).

 

In your reply there isn't the run function. Which function will run when the user clicks the menu.

 

Best regards,

 

 

 

Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: How can I create a MenuItem

Hi,

 

Thank you for your reply.

I tried your solution, here is a picture of the code, it says that super(String arg0,int arg1,int arg2) is deprecated

 

 

Screenshot

 

 

Thank you for your patience...

Contributor
Posts: 34
Registered: ‎10-05-2010
My Device: simulator 9550

Re: How can I create a MenuItem

Hello,

 

Try without the constructor. Delete the first function. Only use the run function.

 

Best regards,

Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: How can I create a MenuItem

If I delete the constructor I get an error the says I must use a constructor.

 

By the way, the constructor of (String, int, int) is deprecated

Contributor
Posts: 34
Registered: ‎10-05-2010
My Device: simulator 9550

Re: How can I create a MenuItem

Have you tried this code?

 

class MyUiScreen extends MainScreen
{
     public MyUiScreen()
     {
       //...
       // Create MenuItem with command context
       MenuItem myItem = new MenuItem(new StringProvider("My Menu Item"), 0x230000, 0);
       myItem.setCommandContext(new Object()
       {
           public String toString()
           {
               return "My MenuItem Object";
           }         
       });
       // Set command to be invoked by the MenuItem
       myItem.setCommand(new Command(new DialogCommandHandler()));
     
       addMenuItem(myItem);
     }

// A CommandHandler implementation which will be executed unconditionally
class DialogCommandHandler extends CommandHandler
{
     public void execute(ReadOnlyCommandMetadata metadata, Object context)
     {
         Dialog.alert("Executing command for " + context.toString());
     }          
}

 

http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/MenuItem.html

 

Best regards,

 

Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: How can I create a MenuItem

I get a RuntimeException
also I have to change setCommand to
myItem.setCommandContext(new Command(new DialogCommandHandler()));
Highlighted
Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: How can I create a MenuItem

It works when I use simulator os version of 6.0, if I use 5.0 it doesn't work.

 

Is there any solution for MenuItem in BlackBerry OS 5.0??