Reputation: 2120
Here is the code I have at the moment:
public class Map extends FragmentActivity implements LocationListener
{
private Location l;
private Location x;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LocationManager provider = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
l = provider.getLastKnownLocation(LocationManager.GPS_PROVIDER);
showm();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date(l.getTime());
Toast.makeText(this, "Fix received on: " + format.format(date), Toast.LENGTH_SHORT).show();
}
void showm()
{
Toast.makeText(this, "Latitude: " + l.getLatitude() + ", Longitude: " + l.getLongitude(), Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location location)
{
if(location != null)
x = location;
}
Now this code works if I run the app the LocationListener method is invoked and GPS fix is received now right after that I call provider.getLastKnownLocation
and according to the Date from Second Toast I got the current location and the showm() method will show a toast with correct Long/Latitude.
Upvotes: 0
Views: 107
Reputation: 2661
showm() should be inside onLocationChanged(Location location).
you are trying to show the location while the query is still in progress. On GPS fix a new location is reported via callback onLocationChanged(...) method. else a NULL(which is the reason for null exception).
Upvotes: 1