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
Developer
ronenfe
Posts: 77
Registered: ‎06-03-2009
My Device: Not Specified
Accepted Solution

open new screen from a current screen

hi, i am new to blackberry.

 

i have 2 files: screen1.java and screen2.java.

 

in screen1.java:

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class Screen1 extends UiApplication
{

public static void main(String[] args)
{
Screen1 screen1 = new Screen1();
screen1.enterEventDispatcher();
}
public Screen1()
{
pushScreen(new Screen1Screen());
Screen2 screen2 = new Screen2();
screen2.enterEventDispatcher();
}
}
final class Screen1Screen extends MainScreen
{


public Screen1Screen()
{
super();
setTitle(new LabelField("Screen1"));
}

}

 in screen2.java

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class Screen2 extends UiApplication
{

public Screen2()
{
pushScreen(new Screen2Screen());
}
}
final class Screen2Screen extends MainScreen
{


public Screen2Screen()
{
super();
setTitle(new LabelField("Screen2"));
}

}

 

i get exception error: Application already running in this process.

 

What am i doing wrong? i want to open screen2 from screen1.

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

Re: open new screen from a current screen

" i am new to blackberry." - Welcome aboard

 

To answer your specific question, you problem is that you seem to think that it is the statement

"enterEventDispatcher();"

that enables new Screens to be pushed.  This is not the case, the enterEventDispatcher is just a bit of magic you have to code at the start of the application to get it going and you only code it once.  You will understand this better latter, for the moment, just code it and continue.

 

I have updated your code in a way that I think will work (but it is not tested and might not even compile).  I've also renamed things in a way that I hope will make their function clear.  This will display screen 1, and when you enter on screen 1, it will take you to screen 2.  I've pasted this below.  Hope it helps.

 

As a newbie, I recommend you spend sometime familiarizing yourself with the resources available from

www.blackberry.com/developers

and this sticky Thread

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&thread.id=13264

 

import net.rim.device.api.ui.component.LabelField; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.system.Characters; public class StartUp extends UiApplication { public static void main(String[] args) { StartUp theApp = new StartUp(); theApp.enterEventDispatcher(); } public StartUp() { this.pushScreen((new Screen1Screen()); } } final class Screen1Screen extends MainScreen { public Screen1Screen() { super(); setTitle(new LabelField("Screen1")); } protected boolean keyChar(char character, int status, int time) { switch (character) { case Characters.ENTER: this.pushScreen((new Screen2Screen()); return true; default: return super.keyChar(character, status, time); } } } final class Screen2Screen extends MainScreen { public Screen2Screen() { super(); setTitle(new LabelField("Screen2")); } }

 

Please use plain text.
Developer
ronenfe
Posts: 77
Registered: ‎06-03-2009
My Device: Not Specified

Re: open new screen from a current screen

Thanks for your great and fast response, based on your solution i realized that to create  a new screen you only need to create a new class derived from mainscreen.

so i changed the code to that and it works:

 

App.java:

 

import net.rim.device.api.ui.UiApplication; public class App extends UiApplication { public static void main(String[] args) { App app = new App(); app.enterEventDispatcher(); } public App() { pushScreen(new Screen1()); pushScreen (new Screen2()); } }

 Screen1.java:

 

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;


final class Screen1 extends MainScreen
{


public Screen1()
{
super();
setTitle(new LabelField("Screen1"));
}
public boolean onClose()
{
setDirty(false);
return super.onClose();
}

}

 Screen2.java:

 

import net.rim.device.api.ui.component.LabelField; import net.rim.device.api.ui.container.MainScreen; final class Screen2 extends MainScreen { public Screen2() { super(); setTitle(new LabelField("Screen2")); } public boolean onClose() { setDirty(false); return super.onClose(); } }

 

 

 

 

 

 


 

 

Please use plain text.
New Contributor
Pusher
Posts: 4
Registered: ‎06-08-2010
My Device: 8830
My Carrier: Verizon

Re: open new screen from a current screen

Why would I get this error whenever I try to push a new screen as in your example.

"The method pushScreen(Screen2Screen) is undefined for the type Screen1Screen".

I have the same problem if I create code on my own or use an example. I know it has to be something simple but I can't figure this one out. Been looking over docs for days.

2178iF6A2B3CD78E4875C

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

Re: open new screen from a current screen

Apologies. 

 

There is a bug in the code I supplied in the previous post.  pushScreen is a method of the UiApplication, not the Screen.  So you need code like:

 

UiApplication.getUiApplication().pushScreen(....);

Please use plain text.