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.

Maveňツ
  • 1
  • 12
  • 50
  • 89
Pierpaolo Ercoli
  • 1,028
  • 2
  • 11
  • 32
  • Is error in this line *GoogleMap map = ((MapSupportFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();* ? – Maveňツ Sep 19 '14 at 07:42
  • exactly that line.. @maven – Pierpaolo Ercoli Sep 19 '14 at 07:44
  • Perhaps you are missing the import statement for com.google.android.gms.maps.SupportMapFragment. – Maveňツ Sep 19 '14 at 07: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 :/ – Pierpaolo Ercoli Sep 19 '14 at 07:48
  • you need to import support jar also – Key_coder Sep 19 '14 at 07:55

1 Answers1

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.
GioLaq
  • 2,489
  • 21
  • 26
  • @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 – Pierpaolo Ercoli Sep 19 '14 at 08:05
  • @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](http://i277.photobucket.com/albums/kk71/ErikaSparrow/Cattura.png) – Pierpaolo Ercoli Sep 19 '14 at 08:24
  • Create a google play service project following steps in http://developer.android.com/google/play-services/setup.html – GioLaq Sep 19 '14 at 08:38
  • Ok, I have to tick the "copy projects into workspace" ?? Sorry but I'm at really beginning with android :/ – Pierpaolo Ercoli Sep 19 '14 at 08:42
  • yes it's better, don't worry we are here for help :) – GioLaq Sep 19 '14 at 08:45
  • Ok many thanks :) - it return an Error: Resource '/HomeActivity/' already exists and in the background I see many error red lines :( – Pierpaolo Ercoli Sep 19 '14 at 08:47
  • here the red lines: [2014-09-19 10:33:36 - RealtimePlaygroundHomeActivity] /RealtimePlaygroundHomeActivity/gen already exists but is not a source folder. Convert to a source folder or rename it. [2014-09-19 10:46:25 - google-play-services_lib] Unable to resolve target 'android-9' [2014-09-19 10:46:26 - MainActivity] Unable to resolve target 'android-9' [2014-09-19 10:46:30 - google-play-services_lib] Unable to resolve target 'android-9' – Pierpaolo Ercoli Sep 19 '14 at 08:47
  • You can try as in this http://stackoverflow.com/questions/17611017/importing-google-play-service-library-showing-a-red-x-next-to-this-reference-and – GioLaq Sep 19 '14 at 08:53
  • Ok understood @JoaoBiriba. My packages I imported don't find the path of the real imports, in fact there are a red cross...now I imported the right path but red cross near packages are still there. :/ – Pierpaolo Ercoli Sep 19 '14 at 08:58
  • @JoaoBiriba we have news. I go deep into javas files and I solved solvable problems that they couldn't find android.support.v4 library and maps libraries and I added manually. But now in every javas they don't find android.support.v7 and the R class. – Pierpaolo Ercoli Sep 19 '14 at 09:07