Quando eu clico no link do mapa eu recebo a pergunta e o crash ao mesmo tempo. Se eu dou a permissão ele não dá o crash novamente, se eu nego a permissão então volta ao mesmo erro.
Detalhe que a tela da permissão fica por trás do aviso de crash e quando dou Ok no aviso ela vem para frente e fica o launcher atrás da tela de permissão.
package br.com.alura.agenda;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.Manifest;
public class MapaActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSOES = 1;
private MapaFragment mapaFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction tx = manager.beginTransaction();
mapaFragment = new MapaFragment();
tx.replace(R.id.frame_mapa, mapaFragment);
tx.commit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] permissoes = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
requestPermissions(permissoes, REQUEST_PERMISSOES);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == REQUEST_PERMISSOES){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED){
new Localizador(this, mapaFragment);
}
}
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_enviar_notas:
new EnviaAlunosTask(this).execute();
break;
case R.id.menu_baixar_provas:
Intent vaiParaProvas = new Intent(this, ProvasActivity.class);
startActivity(vaiParaProvas);
break;
case R.id.menu_mapa:
Intent vaiParaMapa = new Intent(this, MapaActivity.class);
startActivity(vaiParaMapa);
break;
}
return super.onOptionsItemSelected(item);
package br.com.alura.agenda;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
import br.com.alura.agenda.dao.AlunoDAO;
import br.com.alura.agenda.modelo.Aluno;
/**
* Created by Rodrigo on 27/03/2017.
*/
public class MapaFragment extends SupportMapFragment implements OnMapReadyCallback {
private GoogleMap mapa;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.mapa = googleMap;
LatLng posicaoDaEscola = pegaCoordenadaDoEndereco("Rua Vergueiro 3185, Vila Mariana, Sao Paulo");
if (posicaoDaEscola != null){
centralizaEm(posicaoDaEscola);
}
//adiciona local do aluno
AlunoDAO dao = new AlunoDAO(getContext());
for(Aluno aluno : dao.buscaAlunos()){
LatLng coordenada = pegaCoordenadaDoEndereco(aluno.getEndereco());
if(coordenada != null) {
MarkerOptions marcador = new MarkerOptions();
marcador.position(coordenada);
marcador.title(aluno.getNome());
marcador.snippet(String.valueOf(aluno.getNota()));
mapa.addMarker(marcador);
}
}
dao.close();
new Localizador(getContext(), MapaFragment.this);
}
private LatLng pegaCoordenadaDoEndereco(String endereco) {
try{
Geocoder geocoder = new Geocoder(getContext());
List<Address> resultados = geocoder.getFromLocationName(endereco, 1);
if(!resultados.isEmpty()){
LatLng posicao = new LatLng(resultados.get(0).getLatitude(),
resultados.get(0).getLongitude());
return posicao;
}
}catch(IOException e){
e.printStackTrace();
}
return null;
}
public void centralizaEm(LatLng coordenada){
if(mapa != null){
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(coordenada, 17);
mapa.moveCamera(update);
}
}
}
package br.com.alura.agenda;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;
/**
* Created by Rodrigo on 27/03/2017.
*/
public class Localizador implements GoogleApiClient.ConnectionCallbacks, LocationListener {
private final GoogleApiClient client;
private final MapaFragment mapaFragment;
public Localizador(Context context, MapaFragment mapaFragment) {
client = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.build();
client.connect();
this.mapaFragment = mapaFragment;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
LocationRequest request = new LocationRequest();
request.setSmallestDisplacement(50);
request.setInterval(1000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(client, request, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
LatLng coordenada = new LatLng(location.getLatitude(), location.getLongitude());
mapaFragment.centralizaEm(coordenada);
}
}
Olhei nos outros tópicos, mas não consegui achar o problema.