BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 06-15-2010, 08:38 AM   #1
lkr
New Member
 
Join Date: Jun 2010
Model: 9550
PIN: N/A
Carrier: Rogers Wireless
Posts: 3
Default super.close giving illegalstateexception

Please Login to Remove!

okay, this is my first time ever posting, i am writing my first blackberry app. Trying to open a screen that starts a timer and when timer finishes, it closes but i keep getting an illegalstateexception error. Here is the code:

Call to open window (from another window):
TimerScreen ts = new TimerScreen(5);
UiApplication.getUiApplication().pushScreen(ts);

I commented out all my timer stuff since i thought that might be the problem, here is what is left (but i still get the error): RED LINE IS THE PROBLEM

public class TimerScreen extends MainScreen {
private int statusValue = 0;
private Timer timer;
private Vector fields = new Vector();
private TimerTask tt;


public void close() {
System.out.println("junk");
// try {
// timer.cancel();
// }
// catch (Exception e) {}
super.close();
System.out.println("just before pop");
Screen screen=UiApplication.getUiApplication().getActiveS creen();

if (screen instanceof TimerScreen) {
UiApplication.getUiApplication().popScreen(screen) ;
} else
add(new LabelField("junkk"));
}

public boolean isDirty() {
return false;
}

public TimerScreen(int NumOfSeconds) {
// get the MainScreen's VerticalFieldManager...
super();
Manager vfm = this.getMainManager();
timer = new Timer();

//default gauge
final GaugeField gf1;
{
gf1 = new GaugeField("Custom gauge",
0, 100, 0,
GaugeField.NO_TEXT | GaugeField.PERCENT | GaugeField.LABEL_AS_PROGRESS);
// gf1.setLabel("default gauge:");
// gf1.setValue(12);
fields.addElement(gf1);
}

// custom gauge
final GaugeField gf2;
{
gf2 = new GaugeField("custom gauge:",
0, 100, 0,
GaugeField.NO_TEXT | GaugeField.PERCENT | GaugeField.LABEL_AS_PROGRESS);
fields.addElement(gf2);
}

// custom gauge #2
final GaugeField gf3;
{
gf3 = new GaugeField("Picture will display in:",
0, 100, 0,
GaugeField.PERCENT | GaugeField.LABEL_AS_PROGRESS);
fields.addElement(gf3);
}

// custom gauge #3
final GaugeField gf4;
{
gf4 = new GaugeField("At 100% next picture will appear",
0, 100, 0,
GaugeField.LABEL_AS_PROGRESS);
fields.addElement(gf4);
}

// add all the fields to the vfm...
{
// add all the guagefields
for (int i = 0; i < fields.size(); i++) {
Object field = fields.elementAt(i);
vfm.add((Field) field);
}

// add a separator
vfm.add(new SeparatorField(SeparatorField.LINE_HORIZONTAL));
}

System.out.println(":: TimerDemo adding components to the MainScreen...");
/*
tt=new TimerTask() {
public void run() {
if (statusValue >= 100) {
try {
tt.cancel();
timer.cancel();
}
catch (Exception e) {}

System.out.println(
":: TimerDemo shutting down timer (and subsequent timer tasks)... statusValue > 100");
close();

}
else {
statusValue=statusValue+20;
for (int i = 0; i < fields.size(); i++) {
Object field = fields.elementAt(i);
((GaugeField) field).setValue(statusValue);
}
System.out.println(
":: TimerDemo firing a status value update... statusValue=" + statusValue);
}
}
};
*/
// timer.schedule(tt,0,1000);
invalidate();
System.out.println(":: TimerDemo starting timer task...");
close();
}
}// end MainScreen
Offline  
Old 06-15-2010, 10:13 AM   #2
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Default

I'm guessing that you are calling close() from the timer thread. The "IllegalState" is that you are attempting to access the UI from a non-event thread.

To place the close() call in the event thread, you'll need to wrap this in a runnable and call UiApplication.invokeLater(). This queues up the runnable in the event queue, so that it is eventually handled by the event thread.
Offline  
Old 06-15-2010, 12:06 PM   #3
lkr
New Member
 
Join Date: Jun 2010
Model: 9550
PIN: N/A
Carrier: Rogers Wireless
Posts: 3
Default

the only thing is that i thought i had commented out all the stuff to do with the timer and i still get the error (except i did leave in the line timer = new Timer().

I am new to threads, i dont know exactly what you mean by runnable, do you have a piece of sample code?

thanks
Offline  
Old 06-15-2010, 04:04 PM   #4
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Default

Runnable is an interface - see the java docs.

You create a class that extends Runnable, then place your UI update code in the run() method.

Something like this:

UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
myscreen.close();
}
});
Offline  
Old 06-23-2010, 09:55 AM   #5
lkr
New Member
 
Join Date: Jun 2010
Model: 9550
PIN: N/A
Carrier: Rogers Wireless
Posts: 3
Default thanks

i took your advice, i did a search on invokelater and found someone else who did a timer screen differently than me but it worked, so I modified that code and used it instead. thanks for the help. I cant see where to mark this thread as solved though (or do we not do that on this forum?)
Offline  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump






Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.