12-21-2009 02:24 AM - edited 12-21-2009 02:25 AM
Here's the basic idea. I have screenA, where the user can select a button.. Upon selection, ScreenB (a popup) is pushed, where the user chooses between sub-options. Upon choosing they return to screenA. If they choose another button screenB should show again. However, everytime I try to reshow screenB I get an illegalStateException.
I think my problem is that I'm not closing screenB correctly (the methods I tried, I wrote down at the end of the code). Here is some sample code that has the basic idea down.
/* * Test.java */ package test; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.FieldChangeListener; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.component.ButtonField; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.ui.container.VerticalFieldManager; public class Test extends UiApplication { private FieldListener listener = new FieldListener(); public static TestScreen testScreen; public static void main (String args[]) { Test app = new Test(); app.enterEventDispatcher(); } public Test() { //display a new screen testScreen = new TestScreen(); UiApplication.getUiApplication().pushScreen(testSc reen); } ButtonField bf1 = new ButtonField( "Hello World",ButtonField.CONSUME_CLICK); public class TestScreen extends MainScreen { public TestScreen() { super(); VerticalFieldManager vfm = new VerticalFieldManager(); bf1.setChangeListener(listener); add(bf1); add(vfm); } } class FieldListener implements FieldChangeListener { public void fieldChanged(Field button, int context) { TestPopupScreen.popup = new TestPopupScreen(); UiApplication.getUiApplication().pushScreen( TestPopupScreen.popup); } } }
/* * TestPopupScreen.java */ package test; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.FieldChangeListener; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.component.ButtonField; import net.rim.device.api.ui.container.PopupScreen; import net.rim.device.api.ui.container.VerticalFieldManager; public class TestPopupScreen extends PopupScreen { public static TestPopupScreen popup; private static VerticalFieldManager vfm = new VerticalFieldManager(); private FieldListener listener = new FieldListener(); public TestPopupScreen() { super(vfm,Field.FOCUSABLE); ButtonField bf = new ButtonField( "Hello Again",ButtonField.CONSUME_CLICK); bf.setChangeListener(listener); add(bf); } class FieldListener implements FieldChangeListener { public void fieldChanged(Field button, int context) { //What is the proper way to close the screen so it //can be relauched later? //popup.close();// IllegalStateException //UiApplication.getUiApplication().popScreen(popup ); //gives IllegalStateException /* * RuntimeException * UiApplication.getUiApplication().pushScreen(Test.t estScreen); * UiApplication.getUiApplication().popScreen(popup); */ } } }
Thanks in advace for any help. It's very much appreciated.
take care,
kentt
Solved! Go to Solution.
12-21-2009 04:26 AM - edited 12-21-2009 05:16 AM
Welcome!
Haven't looked seriously at your problem, but I think your popup screen should be recoded as follows. Try it, I think your problem will go away.
In general I would be very careful about the use of static variables. You seem to use them in places where they are not needed.
public class TestPopupScreen extends PopupScreen
implements FieldChangeListener
{
private VerticalFieldManager vfm =
new VerticalFieldManager();
public TestPopupScreen()
{
super(vfm,Field.FOCUSABLE);
ButtonField bf = new ButtonField(
"Hello Again",ButtonField.CONSUME_CLICK);
bf.setChangeListener(listener);
add(bf);
}
public void fieldChanged(Field button, int context)
{
UiApplication.getUiApplication().popScreen(this);
}
}
12-21-2009 05:12 AM
Hi peter_strange. Thanks for the advice about static variables. I'll be mindful of that.
As far as the rest of the code goes, I'm a bit confused on how fieldChanged is fired?
12-21-2009 05:18 AM
Apologies, in my code, I have a line commented out. I've fixed the code. Hoepfully it makes more sense now and answers your question.
12-21-2009 05:38 AM
No not quite. I had already uncommented that line. What I am confused about is "bf.setChangeListener(listener);" part. I don't understand how that is implemented.
Thanks for looking,
kentt
12-21-2009 02:02 PM
Okay, so this is what I was trying, but now the fieldChanged() function never runs. I've been poking around on the the internet and trying to see why not but it's baffeling me.
Thanks for looking.
public class TestPopupScreen extends PopupScreen implements FieldChangeListener
{
private static VerticalFieldManager vfm =
new VerticalFieldManager();
public TestPopupScreen()
{
super(vfm,Field.FOCUSABLE);
ButtonField bf = new ButtonField(
"Hello Again",ButtonField.CONSUME_CLICK);
bf.setChangeListener(this);
vfm.add(bf);
add(vfm);
}
public void fieldChanged(Field field, int context)
{
//this never runs
UiApplication.getUiApplication().popScreen(this);
}
}
/*as an off topic note, in Opera 10.10, you cannot insert code into your post*/
12-21-2009 11:43 PM
I have find out your problem. the main reason was for your problem is you can not push same screen object more than once in stack. When you click second hello world button you are pushing the first screen again to show that.this is wrong. you can not do this ..i have attached the code.
package test;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManag
public class Test extends UiApplication
{
private FieldListener listener = new FieldListener();
public static TestScreen testScreen;
public static void main (String args[])
{
Test app = new Test();
app.enterEventDispatcher();
}
public Test()
{
//display a new screen
testScreen = new TestScreen();
UiApplication.getUiApplication().pushScreen(testS
}
ButtonField bf1 = new ButtonField("Hello World",ButtonField.CONSUME_CLICK);
public class TestScreen extends MainScreen
{
public TestScreen()
{
super();
VerticalFieldManager vfm = new VerticalFieldManager();
bf1.setChangeListener(listener);
add(bf1);
add(vfm);
}
}
class FieldListener implements FieldChangeListener
{
public void fieldChanged(Field button, int context)
{
TestPopupScreen.popup = new TestPopupScreen();
UiApplication.getUiApplication().pushScreen(TestP
}
}
}
/////////////
package test;
import test.Test.FieldListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManag
public class TestPopupScreen extends PopupScreen
{
public static TestPopupScreen popup;
public TestPopupScreen()
{
super(new VerticalFieldManager(),Field.FOCUSABLE);
ButtonField bf = new ButtonField("Hello Again",ButtonField.CONSUME_CLICK);
bf.setChangeListener(listener);
add(bf);
}
FieldChangeListener listener= new FieldChangeListener()
{
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen();
}
};
}
12-22-2009 03:32 AM
Oh gosh. Thank you so much.
Got that working and implemented it in my own program.
I'm very grateful that you took the time.
As a small side note, if someone else is looking at this code, the last line should read UiApplication.getUiApplication().popScreen(po
Take care,
kentt
12-22-2009 08:54 AM
good solution thank u one and all
12-22-2009 09:24 AM
To make sure there are no unexpected side effects with multiple instances of this code,
1) I would recommend removing the static TestPopupScreen in TestPopupScreen, and
2) the fieldChanged method in TestPopupScreen should read
UiApplication.getUiApplication().popScreen(this);
not
UiApplication.getUiApplication().popScreen(popup);