Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" />
like image 685
Vishnu Avatar asked Jul 10 '17 12:07

Vishnu


1 Answers

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

like image 147
Jorge L Hernandez Avatar answered Oct 20 '22 06:10

Jorge L Hernandez