I'm at the beginning of programming android apps and I got this error while I try to implement Google Maps into a fragment. I surfed the web and I saw that is a really common problem but I saw also that all other solutions do not work in my case. I paste below the portion of code with all the imports:
public class GeolocalizzazioneFragment extends Fragment { private final LatLng CENTER_POINT = new LatLng(44.22625, 12.06861); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_geolocalizzazione, container, false ); //super.onCreateView(savedInstanceState); //setContentView(R.layout.activity_main); //ottieni il controllo del fragment su cui caricare la mappa GoogleMap map = ((MapSupportFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); //centra la mappa su CENTER_POINT, con zoom 5 //map.moveCamera(CameraUpdateFactory.newLatLngZoom(CENTER_POINT, 5)); //centra la mappa sulla posizione del device map.setMyLocationEnabled(true); //animazione dello zoom sulla nostra animazione all'apertura //Parametri: livello di zoom=10; durata animazione = 1000 millisecondi (1 sec) map.animateCamera(CameraUpdateFactory.zoomTo(10), 1000, null); try{ URL url = new URL("### url where I get xml with location marker datas ###"); //prendo l'URL del file XML dal quale prendo i dati dei POI URLConnection conn = url.openConnection(); //instauro la connessione col file XML DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); org.w3c.dom.Document doc = builder.parse(conn.getInputStream()); NodeList nodes = doc.getElementsByTagName("marker"); for (int i=0; i<nodes.getLength(); i++) { Element item = (Element) nodes.item(i); String name = item.getAttribute("name"); String address = item.getAttribute("address"); String stringLat = item.getAttribute("lat"); String stringLong = item.getAttribute("long"); String icon = item.getAttribute("icon"); //assigned variable for the XML icon attribute Double lat = Double.valueOf(stringLat); Double lon = Double.valueOf(stringLong); // map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); // map.setMyLocationEnabled(true); map.addMarker(new MarkerOptions() .position(new LatLng(lat,lon)) .title(name) .snippet(address) ); } }catch (Exception e){ e.printStackTrace(); } return super.onCreateView(inflater, container, savedInstanceState); } } I also checked that in "Java Build Path" window "Android private libraries" are correctly checked and as you can see I already imported "com.google.android.gms.maps.SupportMapFragment;" and I also imported the "google-play-services.jar" library. And my error is at the line below:
GoogleMap map = ((MapSupportFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); If you need other portions of code, just ask and I will paste :)
P.S.: My IDE is Eclipse.