views:

345

answers:

2

I have this bit of code;

 lm = (LocationManager) 
 getSystemService(Context.LOCATION_SERVICE);

 gpslocation = Double.toString(lm.getLastKnownLocation("gps").getLatitude()) +" " 
 + Double.toString(lm.getLastKnownLocation("gps").getLongitude());

Which works fine on both the emulator and my hero running android 1.5, but it Force Closes on the emulator of 1.6 and also on my tattoo.

What changed from 1.5 to 1.6?

OK, using this instead;

    lm = (LocationManager) 
    getSystemService(Context.LOCATION_SERVICE);

    Double latPoint = null;
    Double lngPoint = null;

    Location loc = lm.getLastKnownLocation("gps");
    if(loc != null) {
       latPoint = lm.getLastKnownLocation("gps").getLatitude();
       lngPoint = lm.getLastKnownLocation("gps").getLongitude();

    } else {

    }

  Toast.makeText(getBaseContext(),"test lat " + latPoint, Toast.LENGTH_LONG).show();

I get null toast, and null toast if i fire a location at the emulator before running the app.

A: 

http://stackoverflow.com/questions/2372980/locationmanager-in-2-1-not-working

I'm having the same problem.. still haven't found a solution.

Every time i do anything with "gps" in my code, it just force closes on android 1.6+

Adam Honoré
+2  A: 

In general, use adb logcat, DDMS, or the DDMS perspective in Eclipse to look at the Java stack trace associated with the "force close" dialog, to see what the problem is.

In your case specifically, it will not work because you have not turned on GPS in the code snippet.

You cannot reliably call getLastKnownLocation() on a provider unless that provider has been up and running. In your case, GPS is probably not running, and getLastKnownLocation() will return null.

You will need to register for location updates or proximity alerts, to get the GPS radio powered on and seeking fixes, before getLastKnownLocation() will work. Also, with the emulator, you will need to submit a fix (e.g., via DDMS) after registering for location updates or something before getLastKnownLocation() will return a non-null value.

CommonsWare
The error was a java lang null pointer error, so i guess it was returning null. I do have a location listener in another class, but here i just wanted to get at the last known location. what changed from 1.5 to 1.6 to make this not work?
arc