Question

I am using Xamarin and am recoding a Google Maps application to use Android.Support.V4.App.Fragments with a View Pager.

Here is my code:

private void InitMapFragment()
{
    _mapFragment = FragmentManager.FindFragmentByTag("map") as MapFragment;
    if (_mapFragment == null)
    {
        GoogleMapOptions mapOptions = new GoogleMapOptions()
            .InvokeMapType(GoogleMap.MapTypeNormal)
            .InvokeZoomControlsEnabled(true)
            .InvokeCompassEnabled(true);

        FragmentTransaction fragTx = FragmentManager.BeginTransaction();
        _mapFragment = MapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.mapWithOverlay, _mapFragment, "map");
        fragTx.Commit();
    }
}

Because I am now extending the Activity to use Android.Support.V4.App.Fragment, I am getting the following errors when compiling:

Error CS0039: Cannot convert type 'Android.Support.V4.App.Fragment' to 'Android.Gms.Maps.MapFragment' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion (CS0039)

At the line:

_mapFragment = FragmentManager.FindFragmentByTag("map") as MapFragment

As well as:

Error CS0029: Cannot implicitly convert type 'Android.Support.V4.App.FragmentTransaction' to 'Android.App.FragmentTransaction' (CS0029)

At the line:

FragmentTransaction fragTx = FragmentManager.BeginTransaction()

Can I have some help to get this code working?

Thanks in advance

Was it helpful?

Solution

Since you are using Android.Support.V4.App.Fragments instead of using MapFragment at

_mapFragment = MapFragment.NewInstance(mapOptions);

use SupportMapFragment

_mapFragment = SupportMapFragment.NewInstance(mapOptions);

Hope that helps!

You should have android.support.v4.jar in your libs folder and in the project build path

OTHER TIPS

You're mixing classes that use the support library fragments with classes that aren't built off that hierarchy (and work only on 3.0+). You can't do that. You need to use SupportMapFragment instead of map fragment.

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