1

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.

5
  • Is error in this line GoogleMap map = ((MapSupportFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); ? Commented Sep 19, 2014 at 7:42
  • exactly that line.. @maven Commented Sep 19, 2014 at 7:44
  • Perhaps you are missing the import statement for com.google.android.gms.maps.SupportMapFragment. Commented Sep 19, 2014 at 7:45
  • No..I imported it, but I think that I solved dividing that code (after 2 hours of attempts). I written this and there are no more errors for SupportMapFragment: MapFragment map =((MapFragment)getFragmentManager().findFragmentById(R.id.map)); GoogleMap mappa = map.getMap(); But now, when I run into my Galaxy note 2, the app crash before start :/ Commented Sep 19, 2014 at 7:48
  • you need to import support jar also Commented Sep 19, 2014 at 7:55

1 Answer 1

1

Try the following steps, maybe you missed some:

  • Copy google play service lib into your lib folder
  • Copy android supported v4 lib in your lib folder
  • Add google play service lib and android supported v4 to your build path
  • import google play service project into your workspace select your project and right click>properties>android>add project lib(the google play service) and do not tick "is library"
  • Select the google play service project and right click and go to properties>android>tick "is lib"
  • Use FragmentActivity and use Support to use supported libs.
  • Clean and rebuild, exit eclipse, uninstall app previously on your device, load eclipse and do another clean and rebuild.
Sign up to request clarification or add additional context in comments.

10 Comments

@JoaoBiriba I'll follow your step and I'll write you the result, thank you so much for a clear step by step guide :D
@JoaoBiriba I'm on the fourth point and when I try to add the project lib folder I can't browse for it. This is a snapshot: link
Create a google play service project following steps in developer.android.com/google/play-services/setup.html
Ok, I have to tick the "copy projects into workspace" ?? Sorry but I'm at really beginning with android :/
yes it's better, don't worry we are here for help :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.