03-10-2012 12:32 PM - edited 03-10-2012 12:38 PM
Here is my main method:
public static void main(String[] args)
{
if (args.length == 1 && args[0].equals("startup"))
{
Criteria locationCriteria = new Criteria();
locationCriteria.setCostAllowed(false);
LocationProvider mlocationProvider;
Location mLocation = null;
try
{
mlocationProvider = LocationProvider
.getInstance(locationCriteria);
mLocation = mlocationProvider.getLocation(-1);
}
catch (LocationException e) {
}
catch (InterruptedException e) {
}
QualifiedCoordinates mQC = mLocation.getQualifiedCoordinates();
}
else
{
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
}
The method
mlocationProvider = LocationProvider.getInstance(locationCriteria);
throws illegal state exception
When I check the debug info I found this exception is thrown at the line when it calls Application.getApplication();
When I move this code to be executed in a normal screen it works fine.!
Any help ?
Solved! Go to Solution.
03-10-2012 02:31 PM
some of the functions need to be called from a GUI thread.
03-10-2012 05:43 PM - edited 03-10-2012 06:41 PM
There are potentially a number of issues here:
1) Until your Application is actually running, you should not really do any processing. Your Application does not start running until you
'enterEventDispatcher()'
So in main, all you should really do is instantiate your Application. Your Application's constructor should not do anything complicated either, since it runs as part of main().
You can do some activities, for example to add listeners, in main() code which is, in some respects, unfortunate because it lulls people into thinking they can do anything. ,
2) Getting the location as you are doing is a Blocking call. So you need to do it on a Background Thread. You c an get away with this on the Simulator because the simulated GPS comes back immediately with a location. So it does not actually block. But on a real device, code such as you have may cause your application to break.
3) You seem to be trying to do something in start-up. You need to be aware, that start-up up is called as part of the device startup and before the the device is fully active. In fact I believe on a real device this code will fail because the device is not ready to provide a location at start up.
You will find find the following KB article enlightening and useful for (1) and (3).
http://supportforums.blackberry.com/t5/Java-Develo
I suspect you will want to start and Application to obtain a location at start up, in which case you might find this useful:
http://supportforums.blackberry.com/t5/Java-Develo
03-10-2012 06:19 PM - edited 03-10-2012 06:19 PM
Peter, you are absolutely right (Y)
Thanks