0

I get "The method getMap() is undefined for the type" on the following code. Why? I have added the Google-Play-Services library.

I have also created this exact same code in an Activity instead of a Fragment and then I didn't get this error. But I neew to use this inside a Fragment

Karta.java:

package nu.sluggo.testapp.foretagsinfo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Karta extends Fragment{



    public static Karta newInstance() {
        Karta f = new Karta();
        return f;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.karta, container, false);
        return v;

    }   

    @Override
    public void onViewCreated(View v, Bundle savedInstanceState){
        super.onViewCreated(v, savedInstanceState);

    final LatLng BUTIKPLATS = new LatLng(57.873873, 11.974995);

    GoogleMap karta;
        karta  = ((Karta) getFragmentManager().findFragmentById(R.id.kontaktVisaFragment)).getMap();

        karta.addMarker(new MarkerOptions().position(BUTIKPLATS).title("Vita Fläckens Blommor"));
        karta.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(BUTIKPLATS, 17);
        karta.animateCamera(update);
    }
    }
Sluggo
  • 597
  • 1
  • 9
  • 21
  • 1
    why not extends `SupprorMapFragment` or `MapFragment` instead? – Emmanuel Jan 16 '14 at 16:58
  • http://stackoverflow.com/questions/20919048/android-android-view-inflateexception-binary-xml-file-line-8-error-inflatin/20971284#20971284. check this – Raghunandan Jan 16 '14 at 16:59

2 Answers2

3

Karta does not extends SupportMapFragment or MapFragment; getMap() is only present in those classes (when dealing with Fragments. You will need to either make Karta extends one of the two (depending on your minSdkVersion) or use:

GoogleMap karta  = ((`SupportMapFragment` or `MapFragment`) getFragmentManager().findFragmentById(R.id.kontaktVisaFragment)).getMap();
Emmanuel
  • 13,083
  • 4
  • 39
  • 53
0

I declare as a global variable

    SupportMapFragment mapFragment

and then in my onCreate() or onCreateView() call

    mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));

if you user SupportMapFragment in your Java implementation make sure to use the same in XML

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />