BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/index.php)
-   Developer Forum (http://www.blackberryforums.com/forumdisplay.php?f=15)
-   -   "Application Already Running in this Process" error with MenuItem (http://www.blackberryforums.com/showthread.php?t=62288)

bdowling 01-22-2007 09:24 AM

"Application Already Running in this Process" error with MenuItem
 
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

bdowling 01-22-2007 11:28 AM

Fixed
 
I managed to solve this one! It involved changing the problem line in the ChangeTelMenuItem class to:

Code:

UiApplication.getUiApplication().pushScreen( new ChangeDetails((Contact)o) );
And changing the ChangeDetails class to:

Code:

public class ChangeDetails extends MainScreen
{
    ChangeDetails (Contact contact)
    {
        super();

        // add title
        LabelField title = new LabelField( "Change .Tel Details", LabelField.ELLIPSIS
            | LabelField.USE_ALL_WIDTH );
        setTitle( title );

        // display contact details
        String[] nameArray = contact.getStringArray( Contact.NAME, 0 );
        String name = nameArray[Contact.NAME_GIVEN] + " " + nameArray[Contact.NAME_FAMILY];
        add( new RichTextField( "Change details for " + name ) );

    }
}

Hope that helps someone else!

Cheers, Ben


All times are GMT -5. The time now is 03:31 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.