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
TaShea
Posts: 24
Registered: ‎07-16-2008
My Device: Not Specified

getLocation() method cannot be called from event thread

Running on JDE 4.2.1 for the T-Mobile 8800 OS 4.2.1.103

 

The program is trying to return the GPS coordinates.  It's a small application that I will add on to later, any help or suggestions would be helpfull.

 

Below is snippet of the code. The code is called from a menu selection.

 

   // Get GPS using Autonomous
     public void sGPS(){
        
         try{            
                   Criteria ct = new Criteria();
                   eType.setText("Autonomous"); 
                  ct.setHorizontalAccuracy(50);
                  ct.setVerticalAccuracy(50);
                  ct.setCostAllowed(true);
                  ct.setPreferredPowerConsumption(Criteria.POWER_USAGE_MEDIUM);
             ct.setPreferredResponseTime(10000);
             LocationProvider lProvider = LocationProvider.getInstance(ct);
            
             Location loc = lProvider.getLocation(-1); // Error occurs here
           
            eCordsLat.setText(String.valueOf(loc.getQualifiedCoordinates().getLatitude()));           
            eCordsLong.setText(String.valueOf(loc.getQualifiedCoordinates().getLongitude()));           
            eStatus.setText("Complete");
           
           
           
            }catch(Exception e){
                System.out.println("Error Msg on geting GPS info : " + e);
                }                                   
       }

 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: getLocation() method cannot be called from event thread

[ Edited ]

What is the exact error? Does it complain about not having the event lock?

If so, you'll see that error when you try to update the UI when running on a seperate thread.

I think the actual error may be the next line after getLocation(), when you call setText()...I'm guessing on a label?

 

See: http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&view=by_date_ascending&messa...

Message Edited by mreed on 07-16-2008 03:55 PM
Please use plain text.
Developer
TaShea
Posts: 24
Registered: ‎07-16-2008
My Device: Not Specified

Re: getLocation() method cannot be called from event thread

The exact error is getLocation() method cannot be called from event thread. 
Please use plain text.
BlackBerry Development Advisor (Retired)
bzubert
Posts: 86
Registered: ‎07-11-2008
My Device: Not Specified

Re: getLocation() method cannot be called from event thread

In this case, you need to make sure that you are not blocking the event thread.  That is, the event thread handles all drawing operations, and getLocation() is a blocking operation.  Calls to getLocation() should be moved to a separate thread or alternatively, you should could use a LocationListener, which manages the threading for you.

 

Please see the following KB article for more information.

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800783/What_Is...

Brian Zubert
Technical Partnership Manager
Research In Motion
Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: getLocation() method cannot be called from event thread

[ Edited ]
Okay. Then you need to get that call off the UI thread. You can use the Timer class to schedule a TimerTask to run in the background. Wrap the UI update portions in an invokeLater() so they will be called on the UI thread.
Message Edited by mreed on 07-16-2008 04:08 PM
Please use plain text.
Developer
TaShea
Posts: 24
Registered: ‎07-16-2008
My Device: Not Specified

Re: getLocation() method cannot be called from event thread

I tried that and for some odd reason, The simulator returns zeros for Lat and Long.. I have values set in the Simulator[8800 T-Mobile] for GPS, but they still come back as Zero. I do get a time stamp, but no Longitude or Latitude..  When you run the simulator with values set for the GPS, you should at least get those returned, right?  Or am I missing something?

Please use plain text.
Administrator
MSohm
Posts: 14,449
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: getLocation() method cannot be called from event thread

The BlackBerry simulator should return the values for the currently selected location that is configured by clicking on the Simulate menu, then GPS Location... and then selecting a location.

 

Have you selected a location?  Or are you simulating a route?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
avinash_kumar_ispg_in
Posts: 172
Registered: ‎01-02-2012
My Device: 8520 curve
My Carrier: compny

Re: getLocation() method cannot be called from event thread

Hi  every one

 

any body got the solution of this problem  if it is please reply me

 

thanks in advance

 

Please use plain text.
Developer
simon_hain
Posts: 16,044
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: getLocation() method cannot be called from event thread

please open an own thread for your question, you can link to this one
----------------------------------------------------------
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
Please use plain text.