You can’t pick up the location so directly, take a look at the documentation: http://developer.android.com/training/location/retrieve-current.html
The moment you are trying to get the location, the device still does not have to provide you, which causes Nullpointer.
I suggest taking a look at the documentation to see how you can implement a Locationlistener, where within it you can check if you have a location to be returned, get location update using the onLocationChanged method and checking changes in the status of the Provider.
An implementation below:
public class Localizacao implements LocationListener {
protected static final String TAG = null;
private Context context;
private LocationManager lm;
private Location location;
private volatile boolean stop = false;
private static final int UM_SEGUNDO = 1000;
private int tempoTotalBusca = 10;
protected ProgressDialog progressDialog;
public Localizacao(Context context) {
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
this.context = context;
}
public boolean estado() {
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
public Location capturarCoordenadaGPS() {
try {
new Thread(new Runnable() {
public void run() {
Looper.myLooper();
Looper.prepare();
progressDialog = ProgressDialog.show(context, null,
context.getString(R.string.aguarde),
true);
ativaGPS();
Looper.loop();
}
}).start();
// Thread.sleep(10*1000);
int tempoBusca = 0;
while (!stop) {
if (tempoTotalBusca == tempoBusca) {
break;
}
Thread.sleep(UM_SEGUNDO);
tempoBusca++;
}
return location;
} catch (Exception e) {
// TODO - Trate a exceção;
} finally {
desativaGPS();
if (progressDialog != null)
progressDialog.dismiss();
}
return null;
}
private void ativaGPS() {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this,
Looper.myLooper());
// Looper.loop();
}
private void desativaGPS() {
lm.removeUpdates(Localizacion.this);
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
stop = true;
}
@Override
public void onProviderDisabled(String provider) {
// Provider desabilitado
}
@Override
public void onProviderEnabled(String provider) {
// Provider habilitado
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// Status do provider alterado
}
}
Could you give some more relevant information here? "replies" only with link tend to become invalid over time if there is any change.
– Maniero
Lucas, I tried to use the class you posted, but I keep getting NULL. <pre><code>public void onCreate() { Locates = new Locates(getApplicationContext()); Location = locates.capturrCoordenadaGPS(); if (Location != null) { la = Location.getLatitude(); lo = Location.getLongitude(); Log. d("create", "la = " + la + " and lo = " + lo); } Else { Log. d("update", "Location null"); } }</code></pre> Any suggestions?
– Matheus Piscioneri
You need to wait for a location to be available, place a breakpoint within the onLocationChanged method, and simulate a change of location using Eclipse Locationcontrols.
– lucasb.aquino
I used the eclipse location change and tested on the mobile. Unsuccessful...
– Matheus Piscioneri
Matheus, try the following test: put a breakpoint inside the onLocationChanged method and then simulate location by Eclipse, everything is correct the execution will fall at breakpoint.
– lucasb.aquino
Lucas, I put the breakpoint and it’s falling right, but the Location is NULL.
– Matheus Piscioneri
Fixing Lucas, on onLocationChanged Location is correct with LA/LO, but the return is NULL.
– Matheus Piscioneri
Well, if it’s correct, just take the property Location. In it will be the coordinates you want. In fact, the onLocationChanged method is void, returns nothing, just arrow the updated Location. So, just access this property.
– lucasb.aquino
Is there any way to call onLocationChanged without actually changing the location? PQ can only get the Location on onLocationChanged can’t get the capture.
– Matheus Piscioneri
Matheus, you must use the getLastKnowLocation() method.
LocationManager locationMngr = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE); 

Location localizacao = locationMngr .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // Aqui o parametro é o seu provider, pode ser NETWORK_PROVIDER ou GPS_PROVIDER.
– lucasb.aquino