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...

  • Tro_SoloTro_Solo DEMember ✭✭

    Okay, so I ran into this problem, too. After some trial and error with the current application, I created a new "Blank XAML App" to play around and check if the error would still occur. The first thing I noticed was that the System.InvalidCastException only occurred on devices (and emulators) running Android Marshmallow (v6.0, SDK level 23).
    Using the solution @frankfitzgerald proposed somehow fixed this problem with the newly created application. So I thought it could be a good idea to check for differences between the two applications.
    Long story short, there were two references missing in my current application which were referenced within the new "Blank XAML App":

    • Xamarin.Android.Support.Vector.Drawable (ver. 23.3.0) and
    • Xamarin.Android.Support.Animated.Vector.Drawable (ver. 23.3.0)

    And guess what? Closing Visual Studio 2015 after adding the references, deleting the bin and obj folders from the working directory again and doing a full rebuild of the current application solved my problem! I am now able to build and deploy the application to my devices and emulators even those running Android Marshmallow. :)

  • @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.