1

I am new to BlackBerry development, having developed iPhone apps for years.

I am trying to do a simple menu like the following, invoked onLoad:

function initMenus() {
  var item = new blackberry.ui.menu.MenuItem(false, 1, "Save", saveMe);
  var item2 = new blackberry.ui.menu.MenuItem(false, 2, "Load", loadMe);
  blackberry.ui.menu.addMenuItem(item);
  blackberry.ui.menu.addMenuItem(item2);
}

function saveMe()
{
  localStorage.pixels = window.pageYOffset;
}

function loadMe()
{
  window.scrollTo(0, localStorage.pixels);
}

In the configuration file, I have:

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns:rim="http://www.blackberry.com/ns/widgets" version="1.0.0" rim:header="RIM-Widget:rim/widget" xmlns="http://www.w3.org/ns/widgets">
  <name>007</name>
  <description>Sample Application created using the BlackBerry Widget SDK that demonstrates how to use the Menu and MenuItem objects found within the Widget API collection.</description>
  <author href="http://na.blackberry.com/eng/developers/browserdev/widgetsdk.jsp" rim:copyright="2010" email="astanley@rim.com">Adam Stanley</author>
  <content src="index.html" />
  <feature id="blackberry.ui.menu" required="true" version="1.0.0.0" />
  <rim:loadingScreen backgroundColor="#c0c0c0" />
  <license href="" />
</widget>

Having installed the WebWorks last SDK, I can try with emulators of OS5 and OS6 families.

Now, with OS6 simulators everything is fine. JavaScript functions are properly called.

With OS5 simulators menu items are shown, but once I click on them, just nothing happens. I tried also with simple alerts, rather than to save and load data in localStorage, but functions are simply not called after clicking menu items.

What am I doing wrong?

I am aware that there is a known issue when declaring blackberry.ui.dialog and blackberry.ui.menu in the same application, but I'm just declaring blackberry.ui.menu.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fabio Ricci
  • 395
  • 1
  • 4
  • 16

1 Answers1

1

I noticed in both of the functions called, you're trying to access localStorage. On OS5, the regular HTML5 localStorage isn't supported, only Google Gears methods. Are you using the html5_init.js in your project? Follow these instructions to add it. It is a Google Gears to HTML5 "converter" for OS5.

That may not be your problem, but you'll need it anyway if you're writing a Webworks app that will work on both version of the OS.

scunliffe
  • 62,582
  • 25
  • 126
  • 161
Core.B
  • 662
  • 3
  • 11
  • Thank you Andrew, you were very kind! Although your answer is very very important and should be pointed out better in their documentation, I really do not use HTML5 (I mean, in this sample yes, but actually I can write everything in those functions). The problem is that they are simply not called. – Fabio Ricci Apr 14 '11 at 08:58
  • **So you tried it and this didn't work?** My guess was that the localStorage doesn't exist so the function calls died when it tried to access an object that didn't exist. Try changing your functions to something simple like `function saveMe() { alert("Save Menu Item Chosen");}` to make sure that the menu item functions are actually being called. – Core.B Apr 14 '11 at 13:02