blackberryjava-mejde

Blackberry MenuItem Deprecated


I am trying to use the MenuItem class of BlackBerry JDE 6.0 and I am encountering an error "The constructor MenuItem String(String, int, int) is deprecated". I am implementing it using a subclass under the MainScreen class. below is the sample deprecated code:

public class UiFunMainScreen extends MainScreen{

   class LoginMenuItem extends MenuItem {

    public LoginMenuItem() {

    super("Login", 20, 10);
    }

    public void run() {
    login();
    }
}
class ClearMenuItem extends MenuItem {

    public ClearMenuItem() {
    super("Clear", 10, 20);
    }

    public void run() {
    clearTextFields();
    }
}
} 

Solution

  • Use following version of code to create a MenuItem:

    class MyUiScreen extends MainScreen
     {
         public MyUiScreen()
         {
             MenuItem myItem = new MenuItem(
                     new StringProvider("My Menu Item"), 
                     0x230000, 
                     0
                 );
     // rest of codes...
    

    from the RIM BlackBerry API 6.0 Documentation

    Creating menu items by subclassing and implementing Runnable

    If subclassing the extending class must implement the Runnable interface, which in turn supports abstract dispatching of menu actions on activation.

    ...
    // setup the menu items
    MenuItem item = new MyMenuItem();
    menu.addItem(item);
    ...
    class MyMenuItem extends MenuItem {
        MyMenuItem() {
            super(MyResourceBundle.getBundle(), MyResource.MY_MENU_ITEM, 0x230000, 0);
        }
        public void run() {
            // do something
        }
    }
    

    Explore the API.