Google Maps v2 Marker Error

Asked

Viewed 102 times

1

I’m trying to put a mark on the map and it’s making a mistake NullPointExeption

Follows the method:

private void setMapaGoogle() {
    if (mapa != null) {
        mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        try{
            LatLng latlng = new LatLng(latitude,longitude);
            **mapa = SupportMapFragment.newInstance(new GoogleMapOptions().zOrderOnTop(true)).getMap();**
            mapa.addMarker(new MarkerOptions()
                    .position(latlng)
                    .title("Onde Estou!")
                    .snippet("")
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_mapa_local_atual))
                    .visible(true));
            // Move a camera para a posição.
            mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),14));
            //Efeito na camera do mapa
            mapa.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            mapa.getUiSettings().setZoomControlsEnabled(false);
            mapa.getUiSettings().setCompassEnabled(false);
            mapa.getUiSettings().setMyLocationButtonEnabled(false);
       } catch (Exception e) {
           StringBuilder sb = new  StringBuilder().append(e.getClass().getSimpleName()); 
           if  (e.getMessage() != null) { 
               sb.append("\n"); 
               sb.append(e.getMessage()); 
           } 
           Log.e("Exeção", sb.toString()); 
           aux = sb.toString();
           // this code write out all message
           Log.e("Exeção","setMapaGoogle", e);
       }
    }else{
        setMapaGoogle();
    }
}

Error occurs on line:

mapa = SupportMapFragment.newInstance(new GoogleMapOptions().zOrderOnTop(true)).getMap(); 
  • 1

    It would be nice if you highlight the line where nullPointerException error occurs, so that people can help you more easily.

  • Opa, foi malz ae. The error ta on the line: map = Supportmapfragment.newInstance(new Googlemapoptions().zOrderOnTop(true)). getMap();

  • When I take this line the system runs smoothly, I just wanted to better understand what it is for, since this stretch I took straight from the Google site and there I found no details on this particular line.

1 answer

2


It is possible that this happens because in your XML file you have:

android:name="com.google.android.gms.maps.Mapfragment"

changes in the java of

 mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();      

for

 mapa = ((**MapFragment**) **getFragmentManager**().findFragmentById(R.id.map)).getMap();

(without the **)

or in the xml of

android:name="com.google.android.gms.maps.Mapfragment"

for

android:name="com.google.android.gms.maps.Supportmapfragment"

you have to be careful when referencing something. Say something if I helped and if this is the error :) If you are not trying to put logcat and xml file here too!

Cumps

Browser other questions tagged

You are not signed in. Login or sign up in order to post.