Xamarin, Cannot convert fragment to V4.App.Fragment

1k views Asked by At

I'm a new when it comes to Xamarin and I've ran into an issue that i could not resolve no matter what i tried That's the error message I've been getting "cannot implicitly convert type 'Fragment1' to 'andriod.support.V4.App.Fragment

This is my MainActivity

public class MainActivity : AppCompatActivity
{
    int count = 1;
    PagerSlidingTabStrip _tabs;
    ViewPager _pager;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Main);

        _pager = FindViewById<ViewPager>(Resource.Id.pager);
        _tabs = FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
        _pager.PageMargin = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 4, Resources.DisplayMetrics);
        _pager.CurrentItem = 0;

        string[] titles = new string[]
            {
                "Fragment1",
                "Fragment2"
            };

        var adapter = new PagerAdapter(SupportFragmentManager, titles);
        _pager.Adapter = adapter;
        _pager.OffscreenPageLimit = titles.Length;
        _tabs.SetViewPager(_pager);
    }
}

this is my pagerAdapter Code

public class PagerAdapter:FragmentPagerAdapter
{
    private string[] Titles;

    public PagerAdapter(FragmentManager fm, string[] titles)
        : base(fm)
    {
        Titles = titles;
    }

    public override ICharSequence GetPageTitleFormatted(int position)
    {

        return new Java.Lang.String(Titles[position]);
    }

    public override int Count
    {
        get { return Titles.Length; }
    }

    public override Fragment GetItem(int position)
    {
        switch (position)
        {
            case 0:
                return new Fragment1();
            case 1:
                return new Fragment2();
            case 2:
            default:
                return new Fragment1();
        }
    }
}

And this is my fragment

public class Fragment1 : Fragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);

        var view = inflater.Inflate(Resource.Layout.Fragment1Layout, null);

        return view;
    }
}

Any help is appreciated

2

There are 2 answers

3
Matthew Vanlandingham On BEST ANSWER

I'm not familiar with Xamarin, but I have been dealing with fragments a lot recently and have run into this issue several times. I found that sometimes when I'm initializing the Fragments, it was using android.app.Fragment instead of using the android.support.v4.app.Fragment

So, wherever you initialize your Fragment(again not familiar with Xamarin), make sure you use android.support.v4.app.Fragment. Hope this helps!

0
Clinton On

That error means you're inheriting from Android.App.Fragment instead of Android.Support.V4.App.Fragment.

You can either use the solution above or you could also use this cleaner declaration using Fragment = Android.Support.V4.App.Fragment;. Place it before or after your using statements, right before the namespace declaration.