1
I’m making an app that shows user coordinates:
This works properly and checks if the GPS is released:
public void verificaGPS(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//GPS desligado ou sem permissão
txtLatitude.setText("sem GPS");
}else{
//GPS OK
txtLatitude.setText("com GPS");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
pegaCoord();
}
}
It works until calling the pegaCoord function():
public void pegaCoord() {
longitude = location.getLongitude();
latitude = location.getLatitude();
txtLatitude.setText("Latitude: " + latitude);
txtLongitude.setText("Longitude: " + longitude);
try {
txtCidade.setText("Cidade: " + buscarEndereco(latitude, longitude).getLocality());
txtEstado.setText("Estado: " + buscarEndereco(latitude, longitude).getAdminArea());
txtPais.setText("País: " + buscarEndereco(latitude, longitude).getCountryName());
txtHora.setText("Sol nasce às: " + String.valueOf(horaSol(latitude,longitude)));
} catch (IOException e) {
Log.i("GPS", e.getMessage());
}
}
The mistake is:
07-28 17:47:39.497 20924-20924/br.com.wiconsultoria.tattwas E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.wiconsultoria.tattwas, PID: 20924
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.wiconsultoria.tattwas/br.com.wiconsultoria.tattwas.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
created the variable at the top, before starting the code.
public class MainActivity extends AppCompatActivity {
public Location location;
public LocationManager locationManager;
public double latitude;
public double longitude;
is right or should I do different?
Can you help me? Hugs.
I changed how I spoke and gave the same mistake :/
– Italo Rodrigo
where I include => Location Location = new Location();
– Italo Rodrigo
I could not start the variables the way you gave me. he asked for some parameters in Location(...) and others. as I’m still a beginner, I couldn’t make it work. Anyway, thanks for the help.
– Italo Rodrigo
Well, that means you need a few things to start using Location. I don’t know if it’s the same command on the other Ides but on Eclipse if you put
Location location = new Location();
and press F3 on top ofnew "Location()"
it will take you to the class constructor and so you can see what kind of parameter it waits to be started.– Julian
I’m using Android Studio
– Italo Rodrigo
Maybe it’s interesting then to take a look at this website, do not know if you already know all shortcuts. And the equivalent of F3 in this case appears to be Ctrl+alt+b
– Julian
still do not know, I’m programming for android a few days. thanks for the tip
– Italo Rodrigo