Why do I get this InvalidCastException Android.Support.Design.Widget.TabLayout

MichaelcMichaelc USMember ✭✭

System.InvalidCastException: Unable to convert instance of type 'Android.Support.Design.Widget.TabLayout' to type 'Android.Support.V7.Widget.Toolbar'.

I'm getting this error and I'm not sure why. It seems to not like something here(all gen code, I haven't added anything here):

` [Activity(Label = "MyApp", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.tabs;
ToolbarResource = Resource.Layout.toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
}`

Posts

  • Anil.ApexAnil.Apex USMember ✭✭

    @Michaelc I'm having the same issue. Found the solution?

    Things I tried:

    Downgrading Xamarin.Forms but error isn't removed.
    Deleting zips from folder: C:\Users\anila\AppData\Local\Xamarin\zips

  • MichaelcMichaelc USMember ✭✭

    I'm using Prism in my forms project and what I ended up doing was creating a new Prism / Xamarin Forms project, update to the latest versions of Xamarin.Forms and Prism nuget packages. Then added the rest of my objects and code. If fixed the problem.

    I'm not sure what happen that caused this to begin with as I hadn't added any new nuget packages or updated any of the existing ones. But nothing I tried worked and I couldn't find another solution.

  • maheshdsmaheshds USMember

    Anyone found a solution to this issue without having to create a whole new project?

    I'm also getting Unable to convert instance of type 'Android.Widget.LinearLayout' to type 'Android.Support.V7.Widget.Toolbar'. error and I have no idea what's causing it. Tried downgrading Xamarin Forms as well, to no avail.

  • maheshdsmaheshds USMember

    Tried running it on a different emulator with one API level lower, and it works. SIGH.

  • frankfitzgeraldfrankfitzgerald USMember ✭✭
    edited January 2017

    Here is what I did to fix the problem. Delete the bin and obj folders. Delete the actual app from the emulator by going into settings and apps and delete the application and redeploy. Everything is working fine now. Originally I had this error

    System.InvalidCastException: Unable to convert instance of type 'Android.Support.Design.Widget.TabLayout' to type 'Android.Support.V7.Widget.Toolbar'.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I have this problem too. Do @frankfitzgerald suggestions but nothig...

  • @frankfitzgerald said:
    Here is what I did to fix the problem. Delete the bin and obj folders. Delete the actual app from the emulator by going into settings and apps and delete the application and redeploy. Everything is working fine now. Originally I had this error

    System.InvalidCastException: Unable to convert instance of type 'Android.Support.Design.Widget.TabLayout' to type 'Android.Support.V7.Widget.Toolbar'.

    This was the solution for me. Thanks

Sign In or Register to comment.