>

Fórum

Fórum da RL System, voltado para analistas, programadores etc. Tudo sobre linguagem de programação e desenvolvimento Full Stack: Banco de Dados, Android, C#, Java, .NET, PHP, Node, Javascript, TypeScript, Front, HTML e muito mais.



Aula 1 - Criando Projeto no Android com GPS - location retorna null


Aula 1 - Criando Projeto no Android com GPS

ao utilizar o Location ele me retorna null, mas nao consigo identificar o problema.
Ao retornar null dá erro no momento do la = location.getLatitude();


public void Salvar_Click(View view) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.

return;
}


Location location = locationManager.getLastKnownLocation(provider);
la = location.getLatitude();
lo = location.getLongitude();



6 Respostas



public void Salvar_Click(View view) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.

return;
}


Location location = locationManager.getLastKnownLocation(provider);
la = location.getLatitude();
lo = location.getLongitude();




O erro que ele dá para mim é simplesmente fechar o app "app vendas parou de funcionar".

Ao debugar verifiquei que a variável location está null
e o erro acontece no momento do la = location.getLatitude();




Estou com o mesmo problema




Alguém teria algum ideia de como eu poderia resolver esse problema?
ainda nao consegui.




Estou realizando os testes pelo celular.
Aplico nele a permissão de GPS.

No androidMAnifest.xml tbm contem as permissoes abaixo:

[XML]




android:debuggable="true"
android:allowBackup="true"
...
[/XML]

Se alguem puder ajudar.
Agradeco.




Abner

Isso acontece porque você não tem ainda uma localização.

Veja a aula 11 do curso de Android Online, que é explicado sobre o LocationListener