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
Trusted Contributor
NGeldenhuys
Posts: 176
Registered: ‎04-12-2012
My Device: Bold 9780 & Curve 8250

method cannot be called from event thread

what I want to do is have the a map open with the click of a buttonfield with two Criteria's, the user must either specify the location and then the image must be added to the location otherwise the Image must be added to the users current location.

The problem I am having is adding both criterias into an if statement in a thread / new thread or even in a FieldChangeListener.

The error I keep getting is: Error in location :javax.microedition.location.LocationException: getLocation() method cannot be cal[0.0] led from event thread [0.0] Error in location :getLocation() method cannot be called from event thread

MY Code:

    FieldChangeListener Listener = new FieldChangeListener() {
    public void fieldChanged(Field field, int context) {
        ButtonField buttonClicked = (ButtonField) field;
        if ((buttonClicked.getLabel()).equals("Push")) {
            CustomMapField mMapField;
            Coordinates mCoordinates;
            BlackBerryCriteria blackBerryCriteria = null;
            BlackBerryLocation blackBerryLocation = null;
            BlackBerryLocationProvider blackBerryLocationProvider = null;
            double Doublelat = 0.0;
            double Doublelng = 0.0;
            blackBerryCriteria = new BlackBerryCriteria();
            if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_CELLSITE)){
                    blackBerryCriteria.setMode(GPSInfo.GPS_MODE_CELLSITE);
            }else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_ASSIST)){
                blackBerryCriteria.setMode(GPSInfo.GPS_MODE_ASSIST);
            }else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_AUTONOMOUS)){
                blackBerryCriteria.setMode(GPSInfo.GPS_MODE_AUTONOMOUS);
            }else{
                blackBerryCriteria.setCostAllowed(true);
                blackBerryCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
            } try {
                blackBerryLocationProvider = (BlackBerryLocationProvider) BlackBerryLocationProvider.getInstance(blackBerryCriteria);
                blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60);
                QualifiedCoordinates qualifiedCoordinates = blackBerryLocation.getQualifiedCoordinates();

                Doublelat = qualifiedCoordinates.getLatitude();
                Doublelng = qualifiedCoordinates.getLongitude();
                mCoordinates = new  Coordinates(Doublelat, Doublelng, 0);
                mMapField = new CustomMapField();
                mMapField.mIcon = Bitmap.getBitmapResource("coin_silver.png");
                mMapField.moveTo(mCoordinates);
                mMapField.setZoom(1);
                add(mMapField);
             }catch(Exception e){
                System.out.println("Debug 5");
                System.out.println("Error in location :"+e.toString());
                System.out.println("Error in location :"+e.getMessage());
             }
        }
    }
};

The error Is with the following line add(mMapField);

Please Help Urgently!

Thank you

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: method cannot be called from event thread

You can't call any blocking operation, like getLocation(), from the event thread.  The GPS stuff should be in a separate thread.

 

Please use plain text.
Trusted Contributor
NGeldenhuys
Posts: 176
Registered: ‎04-12-2012
My Device: Bold 9780 & Curve 8250

Re: method cannot be called from event thread

I tried adding it into another thread but it didnt work?

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: method cannot be called from event thread

Can you be more specific than "didn't work"?

 

Please use plain text.