Question

public class PlaceMapsFragment extends SupportMapFragment {
    private GoogleMap mMap;

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


        setUpMapIfNeeded();
        return v;
    }

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((MapView) findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
                "Marker"));
    }
}

Error

The method findViewById(int) is undefined for the type PlaceMapsFragment
Was it helpful?

Solution

If I remember correctly, All Fragments do not have a native findViewById() method. This is why you have to use the onCreateView() method.

This means that you should change setUpMapIfNeeded() to setUpMapIfNeeded(View view) and in onCreateView() pass off v to your modified method. You also need to change mMap = ((MapView) findViewById(R.id.map)).getMap(); to mMap = ((MapView) view.findViewById(R.id.map)).getMap();.

OTHER TIPS

I'm not sure you can subclass the SupportMapFragment class.

To get a new instance of the SupportMapFragment you call SupportMapFragment.newInstance and this returns your object.

If you subclass it and call, for example, PlaceMapsFragment.newInstance() you're still going to be returning a SupportMapFragment object.

If you want to manipulate the map programmatically, I suggest you do the following.

this.mSupportMapFragment = SupportMapFragment.newInstance();
MapHandler mMapHandler = new mMapHandler(this.mSupportMapFragment.getMap());

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top