Vishnu
Vishnu

Reputation: 2207

Xamarin:Unable to convert instance of type Android.Widget.LinearLayout to type Android.Support.V7.Widget.Toolbar

The app is running on Android 6 or below. But when running on Android 7 (Nougat) it throws runtime exception.

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

The error is thrown in Xamarin (Visual studio 2017) at below code:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        ToolbarResource = Resource.Layout.toolbar;
        TabLayoutResource = Resource.Layout.tabs;

        base.OnCreate(bundle); //Runtime error here
        .... Other code ...
    }
}

I've taken latest updates for All Nuget packages but not able to identify the issue. For testing I'm using Moto G4+ (Android 7.0). Can anybody help please?

Edit 1:

Layout of toolbar is as : (Under resources > layout > toolbar.axml). This was already there when App version with Android 6 released. But Now visited app back for Android 7 issues.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar" 
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Upvotes: 11

Views: 7390

Answers (4)

Windgate
Windgate

Reputation: 428

You get this kind of error when trying to assign a control (a UI element in your xml) on a variable that is not of the same type. I get this error frequently when I make a mess and try to assign an EditText to a TextView variable or vice versa, i. e

EditText myText = FindViewById<TextView>(Resource.Id.myText);

or

TextView myText = FindViewById<EditText>(Resource.Id.myText);

I'm pretty sure that in your case you are trying to assign an Android.Widget.LinearLayout to a variable of type Android.Support.V7.Widget.Toolbar.

I think in your case one of this variables have been defined as LinearLayout in your xml:

Resource.Layout.toolbar
Resource.Layout.tabs

Meanwhile, one of this variables have beeen defined as Toolbar in your C# code, so te cast is not possible:

ToolbarResource 
TabLayoutResource 

If the problem persist, you can also try to check the type Android.Support.V7.Widget.Toolbar, instead it could be Android.Widget.Toolbar in your xml or code.

Upvotes: 0

Bondolin
Bondolin

Reputation: 3121

I tried Señor Hernandez's answer without much success, then realized I had accidentally deleted Resources/Resource.designer.cs from my Android project. I had meant to delete it from disk only, expecting it to get regenerated on rebuild. It did get regenerated, but that did not add it back to the project. Doing so (right-click, include in project) solved the issue on my part.

Upvotes: 1

Jorge L Hernandez
Jorge L Hernandez

Reputation: 939

I solved this by just cleaning the entire solution and closing the emulator. Then deploy again and it should work.

Upvotes: 15

Vishnu
Vishnu

Reputation: 2207

It got solved by downloading android_m2repository_r29.zip repository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ with instruction given in same.

I also tested the issue by creating a new fresh solution and got some style "Resource not found error". That was also fixed and then the original problem also got fixed.

Upvotes: 0

Related Questions