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
New Developer
gaston84
Posts: 10
Registered: ‎12-16-2008
My Device: Not Specified

Uncaught exception: pushModalScreen called by a non-event treahd

Hello!.

I am following an example ( "GPSDEMO") in JDE 4.1, to get the coordinates, latitude and longitude.

This example works on my simulator (BB 7520), returns the latitude and longitude that I've loaded in the simulator.
But when I run the application on my (real device), this does not work.
Gives me the following error: "Uncaught exception: pushModalScreen called by a non-event treahd.
My divice is BB 7290.
Version: v4.1.0.309
MPD-2.0
JWTI 1.0


This is the application code:

package testGps;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import javax.microedition.location.Criteria;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.Location;
import javax.microedition.location.Coordinates;
import javax.microedition.location.*;
import java.lang.InterruptedException;
import java.lang.SecurityException;
import java.lang.IllegalArgumentException;
import java.lang.InterruptedException;
import java.lang.NullPointerException;


class gps extends UiApplication {
private static int _interval = 1;
public static void main(String[] args){
gps aplicacion = new gps();
//Método responsable de que la aplicacion comience a procesar mensajes y eventos
aplicacion.enterEventDispatcher();
//javax.microedition.location.Criteria
}

public gps() {
gpsPantalla pantalla = new gpsPantalla();
//Hace mostrar la pantalla asociada a la clase HolaMundoPantalla al arrancar la aplicación
UiApplication.getUiApplication().pushScreen(pantalla);
}


final class gpsPantalla extends MainScreen{
private Location location;
private LocationProvider lp;
public gpsPantalla(){
super();
LabelField titulo = new LabelField("GPSTest",LabelField.USE_ALL_WIDTH);
setTitle(titulo);
add(new RichTextField("Gps! Test"));
// Establecemos los criterios. Queremos sólo un GPS interno o Bluetooth
//Criteria criteria = new Criteria();
//criteria.setCostAllowed(true);
//LocationProvider lp = LocationProvider.getInstance(criteria);
//Si a la sentencia anterior le paso null, indico, que no
// quiero ninguna restriccion!, el LP puede ser cuelquiera.
try {
System.out.println("Buscando proveedor");
add(new RichTextField("Buscando proveedor"));
lp = LocationProvider.getInstance(null);
if (lp == null){
System.out.println("El proveedor es Null!!!");
add(new RichTextField("El proveedor es Null!!!"));
Runnable showGpsUnsupportedDialog = new Runnable() {
public void run() {
Dialog.alert("Este dispositivo no soporta GPS...");
System.exit( 0 );
}
};
invokeLater( showGpsUnsupportedDialog ); // ask event-dispatcher thread to display dialog ASAP


}
lp.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
} catch (LocationException le) {
System.err.println(" No existe proveedor! (Le)");
add(new RichTextField(" No existe proveedor! (Le)"));
System.err.println(le);
System.exit(0);
}catch (NullPointerException npe) {
System.err.println(" No existe proveedor! (Npe) ");
add(new RichTextField(" No existe proveedor! (Npe) "));
System.err.println(npe);
System.exit(0);
}


try{
System.out.println("Setando el tiempo de refresco de geoposecionamiento!!");
add(new RichTextField("Setando el tiempo de refresco de geoposecionamiento!!"));
// indicamos el timeout por defecto
location = lp.getLocation(60);

}catch (LocationException le) {
System.err.println(" Error al solicitar posicionamiento! (Le) ");
add(new RichTextField("Error al solicitar posicionamiento! (Le)"));
System.err.println(le);
//System.exit(0);

}catch(SecurityException se){
System.err.println(" Error de seguridad! (Se) ");
add(new RichTextField("Error de seguridad! (Se)"));
System.err.println(se);
//System.exit(0);
}catch(IllegalArgumentException iae){
System.err.println(" Error en los argumentos! (Iae) ");
add(new RichTextField("Error en los argumentos! (Iae)"));
System.err.println(iae);
//System.exit(0);
}catch(InterruptedException ie){
System.err.println(" Servicio Interrumpido! (Ie) ");
add(new RichTextField("Servicio Interrumpido! (Ie)"));
System.err.println(ie);
//System.exit(0);
}catch (NullPointerException npe) {
System.err.println(" No existe proveedor! No se puede pedir Posicion! (Npe)");
add(new RichTextField("No existe proveedor! No se puede pedir Posicion! (Npe)"));
System.err.println(npe);
//System.exit(0);
}

Coordinates coordinates = location.getQualifiedCoordinates();
if (location != null){
add(new RichTextField("Latitud: " + coordinates.getLatitude()));
add(new RichTextField("Longitud: " + coordinates.getLongitude()));
}

}
}

/*
Implementation of the LocationListener interface.
*/
private class LocationListenerImpl implements LocationListener{
// Members. ————————————————————–
private int captureCount;
private double longitude;
private double latitude;
// Methods. ————————————————————–
public void locationUpdated(LocationProvider provider, Location location){
if(location.isValid()){
longitude = location.getQualifiedCoordinates().getLongitude();
latitude = location.getQualifiedCoordinates().getLatitude();
float altitude = location.getQualifiedCoordinates().getAltitude();
// Information to display on the device.
StringBuffer sb = new StringBuffer();
sb.append("Longitud: " + longitude+ "\n");
System.out.println("Longitud: " + longitude+ "\n");
Dialog.alert("Latitud: " + latitude+ "\n");
sb.append("Latitud: " + latitude+ "\n");
System.out.println("Latitud: " + latitude+ "\n");
//GPSDemo.this.updateLocationScreen(sb.toString());
}
}
public double getLatitud(){
return(this.latitude);
}
public double getLontud(){
return(this.longitude);
}
public void providerStateChanged(LocationProvider provider, int newState){
// No operation defined.
}
}


}

 

I don't know what's the problem, Could you help me, please ?

 

Thanks!!!

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Uncaught exception: pushModalScreen called by a non-event treahd

if you modify the UI from a non-event-thread you have to either synchronize on the event thread or use invokelater to put it into the event threads execution queue.
see the developer guide for details.
----------------------------------------------------------
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
New Developer
gaston84
Posts: 10
Registered: ‎12-16-2008
My Device: Not Specified

Re: Uncaught exception: pushModalScreen called by a non-event treahd

Hello Simon!!

I don't understand you very well.
I use "invokeLater" in the method "updateLocationScreen". It is called from the class
"LocationListenerImpl" and this class implements LocationListener.

You can help me, a little more please!. Thank you a lot!!
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Uncaught exception: pushModalScreen called by a non-event treahd

you have to encapsulate ANY UI interaction that is not done from your event thread.

in your catch-block, for example, you are adding a field. as the exception is thrown by the LocationProvider you are not within the event thread of the application.

If you set breakpoints and debug step by step you can see where your code exits, this will help you the next time.
----------------------------------------------------------
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
New Developer
gaston84
Posts: 10
Registered: ‎12-16-2008
My Device: Not Specified

Re: Uncaught exception: pushModalScreen called by a non-event treahd

This errors occurs only on my device. But in my simulator the application is working (the application hasn't problem) so I can't debug.
I put a new exception and this error occurs again.

 

Sorry for the inconvenience!!

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Uncaught exception: pushModalScreen called by a non-event treahd

you can debug on the device as well.
in the jde, choose debug -> attach to -> device.

pushModalScreen means that one of your Dialog.alert calls is the suspect.
If you change them to sysout your code will run (or at least throw a different exception :smileywink: )

honestly, i don't want to read all this unformatted code. the error is caused by pushing a modal screen (a dialog) from a non-event thread, for example from the listener that is registered on the position provider.
surround it with invokelater and it will work.
----------------------------------------------------------
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
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Uncaught exception: pushModalScreen called by a non-event treahd

[ Edited ]

Removed becuase comments made in this were nearly identical to previous post by simon_hain.

Message Edited by peter_strange on 01-23-2009 04:20 PM
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Uncaught exception: pushModalScreen called by a non-event treahd

i am getting faster because it is only 20 minutes till work is over :smileyhappy:
(and parsing SOAP via BES-Push is finally working in my event system, yeah!)
----------------------------------------------------------
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