I've written a blackberry application that has three different menu items. For one of the menu items I wish an application to be launched with the contact details of the selected contact when the menu was clicked.
Here is my main method:
Code:
public class App
{
public static void main (String[] args)
{
// register all of the menu items
(new ChangeTelMenuItem()).registerInstance();
(new BluetoothSyncMenuItem()).registerInstance();
(new LookupTelMenuItem()).registerInstance();
}
}
Here is the ChangeTelMenuItem code:
Code:
package com.roke.blackberry;
import javax.microedition.pim.Contact;
import net.rim.blackberry.api.menuitem.ApplicationMenuItem;
import net.rim.blackberry.api.menuitem.ApplicationMenuItemRepository;
import net.rim.device.api.ui.component.Dialog;
public class ChangeTelMenuItem extends ApplicationMenuItem
{
ChangeTelMenuItem ()
{
super( 0 );
}
public void registerInstance ()
{
ApplicationMenuItemRepository a = ApplicationMenuItemRepository.getInstance();
a.addMenuItem(ApplicationMenuItemRepository.MENUITEM_ADDRESSBOOK_LIST, this );
a.addMenuItem(ApplicationMenuItemRepository.MENUITEM_ADDRESSCARD_EDIT, this );
}
public Object run (Object o)
{
if (o instanceof Contact)
{
// load the application to modify the details!
ChangeDetails cd = new ChangeDetails((Contact)o); // PROBLEM ON THIS LINE
cd.enterEventDispatcher();
}
else
{
Dialog.alert( "NOT contact" );
}
return null;
}
public String toString ()
{
return "Change Tel Details";
}
}
I've added a comment on the line that causes a runtime exception, with the message application already running in this process. The code for ChangeDetails is:
Code:
public class ChangeDetails extends UiApplication
{
public ChangeDetails (Contact contact)
{
pushScreen( new ChangeDetailsScreen(contact) );
}
private class ChangeDetailsScreen extends MainScreen
{
ChangeDetailsScreen (Contact contact)
{
super();
// add title
LabelField title = new LabelField( "Change Tel Details", LabelField.ELLIPSIS
| LabelField.USE_ALL_WIDTH );
setTitle(title);
// display contact details
add(new RichTextField(contact.toString()));
}
}
}
I've seen on another forum post that one way around this is to init the cd class outside of the run method - but then it wouldn't be possible for me to pass it the contact object.
Any ideas on how I could get this working?
Thanks in advance,
Ben