I'm making an app for people with addresses. Every time I open the map by setting the initial location with the cell phone's GPS, the app hangs and closes.
In code analysis I noticed that the map
variable is getting null, causing hard close
in the application. I need help identifying and fixing the error.
public class MapaFragment extends SupportMapFragment {
private Context context;
private GoogleMap map;
private AlunoDAO dao;
private Localizador localizador;
@Override
public void onResume() {
super.onResume();
context = getActivity();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
addMarkers(map);
}
});
}
public void centralizaNo(LatLng local) {
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(local, 17);
if (update != null) {
map.animateCamera(update);
}
}
private void addMarkers(GoogleMap map){
dao = new AlunoDAO(context);
List<Aluno> alunos = dao.getLista();
localizador = new Localizador(getActivity());
for (Aluno aluno : alunos) {
LatLng localAluno = localizador.getCoordenada(aluno.getRua() + "," + aluno.getNumero() + "-" + aluno.getBairro() + "," + aluno.getCidade());
if (localAluno != null) {
MarkerOptions options = new MarkerOptions().title(aluno.getNome()).position(localAluno);
map.addMarker(options);
}
}
dao.close();
}
}
Logcat:
05-25 16:15:45.787 2425-2425/cadastro.grupotmt.com.br.cadastrotmt E/SysUtils﹕ ApplicationContext is null in ApplicationStatus
05-25 16:15:51.523 2425-2425/cadastro.grupotmt.com.br.cadastrotmt E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: cadastro.grupotmt.com.br.cadastrotmt, PID: 2425
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.animateCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at cadastro.grupotmt.com.br.cadastrotmt.MapaFragment.centralizaNo(MapaFragment.java:49)
at cadastro.grupotmt.com.br.cadastrotmt.AtualizadorDePosicao.onLocationChanged(AtualizadorDePosicao.java:42)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Position updater:
public class AtualizadorDePosicao implements LocationListener {
private LocationManager locationManager;
private MapaFragment mapa;
public AtualizadorDePosicao(Activity activity, MapaFragment mapa) {
this.mapa = mapa;
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
long tempoMinimo = 20000;
float distanciaMinima = 20;
locationManager.requestLocationUpdates(provider, tempoMinimo, distanciaMinima, this);
}
public void cancelar() {
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location novaLocalizacao) {
double latitude = novaLocalizacao.getLatitude();
double longitude = novaLocalizacao.getLongitude();
LatLng local = new LatLng(latitude, longitude);
mapa.centralizaNo(local);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/background_material_light"
android:name="com.google.android.gms.maps.MapFragment">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapa">
</WebView>
</LinearLayout>