Search code examples
xamarinxamarin.formsxamarin.androidandroid-support-libraryandroidx

Xamarin AndroidX Migration: Crash on App Startup (System.InvalidCastException: AndroidX.AppCompat.Widget.Toolbar)


I recently updated my App to Xamarin Forms 5.0 and therefore to AndroidX. I upgraded all packages, which still referenced the old Android Support Libraries and replaced all occurrences of Android Support Library References in my code with the equivalent reference from AndroidX.

But my App keeps crashing on startup, with following exception:

[AndroidRuntime] android.runtime.JavaProxyThrowable: System.InvalidCastException: Unable to convert instance of type 'Android.Widget.LinearLayout' to type 'AndroidX.AppCompat.Widget.Toolbar'.
[AndroidRuntime]   at Java.Interop.JavaObjectExtensions.CastClass (Android.Runtime.IJavaObject instance, System.Type resultType) [0x0005d] in <1410c4afb1fd4823a12ecf897dd92287>:0 
[AndroidRuntime]   at Java.Interop.JavaObjectExtensions._JavaCast[TResult] (Android.Runtime.IJavaObject instance) [0x00052] in <1410c4afb1fd4823a12ecf897dd92287>:0 
[AndroidRuntime]   at Java.Interop.JavaObjectExtensions.JavaCast[TResult] (Android.Runtime.IJavaObject instance) [0x00000] in <1410c4afb1fd4823a12ecf897dd92287>:0 
[AndroidRuntime]   at Android.Runtime.Extensions.JavaCast[TResult] (Android.Runtime.IJavaObject instance) [0x00000] in <1410c4afb1fd4823a12ecf897dd92287>:0 
[AndroidRuntime]   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnCreate (Android.OS.Bundle savedInstanceState, Xamarin.Forms.Platform.Android.ActivationFlags flags) [0x00090] in <04c545f414d24a37af95d995791bb9a9>:0 
[AndroidRuntime]   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00000] in <04c545f414d24a37af95d995791bb9a9>:0 
[AndroidRuntime]   at MyAppDemo.Droid.MainActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00000] in <bdc0728e30d74980ad82869dc2b7bae4>:0 
[AndroidRuntime]   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <1410c4afb1fd4823a12ecf897dd92287>:0 
[AndroidRuntime]   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.3(intptr,intptr,intptr)
[AndroidRuntime]    at crc648c69828764ca3d7c.MainActivity.n_onCreate(Native Method)
[AndroidRuntime]    at crc648c69828764ca3d7c.MainActivity.onCreate(MainActivity.java:43)
[AndroidRuntime]    at android.app.Activity.performCreate(Activity.java:8000)
[AndroidRuntime]    at android.app.Activity.performCreate(Activity.java:7984)
[AndroidRuntime]    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
[AndroidRuntime]    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3404)
[AndroidRuntime]    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
[AndroidRuntime]    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
[AndroidRuntime]    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
[AndroidRuntime]    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
[AndroidRuntime]    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
[AndroidRuntime]    at android.os.Handler.dispatchMessage(Handler.java:106)
[AndroidRuntime]    at android.os.Looper.loop(Looper.java:223)
[AndroidRuntime]    at android.app.ActivityThread.main(ActivityThread.java:7660)
[AndroidRuntime]    at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime]    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
[AndroidRuntime]    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

This is my MainActivity.OnCreate(), which is mentioned in the exception message:

[Activity(ResizeableActivity = true, Label = “My App”, Icon = "@drawable/ic_launcher", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, Theme = "@style/MyTheme", LaunchMode = LaunchMode.SingleTop)]
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        ToolbarResource = Resource.Layout.toolbar;
        TabLayoutResource = Resource.Layout.tabs;
        
        ...
    }
}

And this is my toolbar (Under Resources>layout>toolbar.axml):

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar      // <- before replacing: 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"
    app:layout_scrollFlags="scroll|enterAlways" />

I studied the AndroidX Migration Tutorials, did a lot of research in different forums. I found people with similar issues, but for them uninstalling the app from the device and deleting /bin and /obj folders helped. For me nothing fixed it at all.

(Btw: My iOS Version of the App is running, of course)


Solution

  • Solution: Remove the toolbar.axml would fix this error.