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
Contributor
razebond
Posts: 30
Registered: ‎06-22-2010
My Device: bold 9700

Illegal State Exception : Invoking "Activate Speakerphone" menu

Hi..

Sorry for my english...

 

I'm developing application that simulating the call screen.

 

The application 'll come to fore ground after a call is answered.

 

This works fine, I got the my screen after call is answered.

 

But I want to activate the speaker phone while i click the button on the screen.

 

I'm taking all the menuItems and trying to invoke the "Activate Speakerphone" menu. I can get all the menus including "Activate Speakerphone. But I'm getting the IllegalStateException while run the menu.

 

The application flow is

 

i)Start the application( running in background)

ii)call coming and answered

iii)Application comes to foreground and Showing the screen.

iv)Click the button, should activate speakerphone...

 

 

my code is 

 

 

public void fieldChanged(Field field, int context) {
	if(field==bmpFldconfirm){
	        runMenuItem("Activate Speakerphone");
	}
}

private void runMenuItem(String menuItemText) {
try {
	Screen screen = Ui.getUiEngine().getActiveScreen();
	Menu menu = screen.getMenu(0);
	for (int i = 0, cnt = menu.getSize(); i < cnt; i++){
		if(menu.getItem(i).toString().equalsIgnoreCase(menuItemText)){
			menu.getItem(i).run(); //error in this line
		}
	}
} catch (Exception e) {
	e.printStackTrace();
}
                
   }

 

I don't  get it I've tried everything 

 

 

synchronized(UiApplication.getApplication().getEventLock()){
//Run code
}
synchronized(UiApplication.getApplication().getAppEventLock()){
//Run Code
}
UiApplication.getUiApplication().invokeLater(new Runnable(){ 
public void run(){
//Run Code
}
}
UiApplication.getUiApplication().invokeAndWait(new Runnable(){ 
public void run(){
//Run Code
}
}

 

 

I gets blocked in this... pls help me...

 

 

 

 

 

 

 

 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

what is the message that you get with the exception?

Please use plain text.
Contributor
razebond
Posts: 30
Registered: ‎06-22-2010
My Device: bold 9700

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

I'm getting "null" in catch block.. 

 

If I remove the try-catch block, I'm getting the error and eclipse going to debugging mode and showing the following

 

 

Thread [DriverProtector(202)id=166289408] (Suspended (exception IllegalStateException))
 SystemMenuItem.run() line: 48
 InCallScreen.runMenuItem(String) line: 193
 InCallScreen.fieldChanged(Field, int) line: 171

 

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

Not an area of expertise, here is how I would attempt to resolve this problem, but no promises....

 

If you debug this and trap the exception, then drill down into the exception you should find a detail message, that will give you more information.  This is what Martin was asking you for.  I think the same information is also printed if you use a statement like the following:

System.out.println("Exception: " + e.toString());

 

In general it is perfectly permissible to run menu items directly from the fieldChanged event as this listener runs on the Event Thread.  However the 'worrying' thing in your specific case is this line:

 

Screen screen = Ui.getUiEngine().getActiveScreen();

I am not sure that you are getting a Screen that is owned by your application, I suspect you are getting a Screen that is owned by the Phone and trying to run in your context.  I suspect that is what your detail message will tell you.

 

So I would consider changing your code to the following, which might help:

 

Screen screen = Ui.getUiEngine().getActiveScreen();

Application screenAppl = screen.getApplication();
Menu menu = screen.getMenu(0);
for (int i = 0, cnt = menu.getSize(); i < cnt; i++){
if(menu.getItem(i).toString().equalsIgnoreCase(menuItemText)){

screenAppl.invokeLater(new Runnable(){
    public void run(){
        menu.getItem(i).run();
    }
}}
}

 

One final thing, you will not get a stack trace from this:

} catch (Exception e) {
e.printStackTrace();
}

Change it to this:

} catch (Throwable t) {
t.printStackTrace();
}

 

Please use plain text.
Developer
simon_hain
Posts: 16,209
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

I retrieve a reference to the phone screen in my phone listener and publish it to my application using a global event.

i would suggest that you do not execute anything inside a phonelistener, just post a global event and receive it in your own app.

----------------------------------------------------------
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
Please use plain text.
Contributor
razebond
Posts: 30
Registered: ‎06-22-2010
My Device: bold 9700

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

Thanks for your replay.. I tried your code, Still I'm getting the same error.. :smileysad:  

 

I'm getting the error..

java.lang.IllegalStateException

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

Can you give us the detail message for the Exception.  See the start of my last post for more information. 

Please use plain text.
Developer
spring_suvarna
Posts: 330
Registered: ‎02-25-2009
My Device: Not Specified

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

Hi Peter

 

I am getting the same exception when i try to execute "menu.getItem(i).run()".

 

I check exception object and it has got no detailed information, backtrace = null and detailedMessage = null

 

when i get a call after user answrs it i try to hold it. so for this i am accessing Hold menu and try to run it through code. but it gives IllegalStateException

 

anyone ??

 

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

Which listener are you trying to run this from?

 

Have you checked that the listener is running with the Event lock?

 

If it is not, then try running the menu inside something like

UiApplication.getUiApplication.invokeLater(new Runnable() {

public void run() {

...

}

});

 

I would use invokeLater rather than any other mechanism, because I suspect you don't need to wait for this to happen to proceed with your processing. 

 

Having said that, I have never tried to influence the behavior of the phone in this way.  I've only ever captured information from the Phone call, using a mechanism like that suggested by Simon earlier on in this Thread. 

Please use plain text.
Developer
spring_suvarna
Posts: 330
Registered: ‎02-25-2009
My Device: Not Specified

Re: Illegal State Exception : Invoking &quot;Activate Speakerphone&quot; menu

Basically i want to hold a call programatically.

 

what i am doing is :

- i have implemented PhoneListener. When user answers a call, callAnswered(int callId) gets invoked.

- I am throwing GlobalEvent from here

- My Application catches this global enent and then execute following code

 

try {
    Screen screen = Ui.getUiEngine().getActiveScreen();//getActiveScreen();
    Application screenAppl = screen.getApplication();
    Menu menu = screen.getMenu(0);
    int menuSize = menu.getSize();
    log("menuSize " + menuSize);
    for (int i = 0; i < menuSize; i++) {
	final MenuItem menuItem = menu.getItem(i);
	String menuString = menuItem.toString();
	log(i + " " + menuString);
	if (menuString.equalsIgnoreCase("Hold")) {
 	    screenAppl.invokeLater(
	        new Runnable() {
	            public void run() {
		        try {		
		            menuItem.run();
	                } catch (Exception e) {
			    log(e.toString() + " " + e.getMessage());										
		        }
		    }
	        }
	    );
	}
    }
} catch (Exception e) {
    log(e.toString());
}

 

here,

menuItem.run();

this throws exception "IllegalStateExcption"

 

What could be the reason for this?

 

 

Please use plain text.