Search code examples
c#androidxamarinxamarin.androidmono

System.TypeLoadException: 'VTable setup of type AndroidX.AppCompat.View.Menu.MenuItemImpl failed'


I am attempting to upgrade a Xamarin Android app to AndroidX. I went through the migration process and the project builds without errors, but at runtime I am hitting an issue with IMenu. I have an activity that calls OnCreateOptionsMenu(IMenu menu), when it gets to menu.FindItem, System.TypeLoadException: 'VTable setup of type AndroidX.AppCompat.View.Menu.MenuItemImpl failed' is being thrown.

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.common_menu, menu);
            menu.FindItem(Resource.Id.action_settings).SetVisible(false); // Exception happens here.
            return true;
        }

Stack trace:

  at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x0002c] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:951 
  at System.Reflection.RuntimeConstructorInfo.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00086] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:926 
  at System.Reflection.RuntimeConstructorInfo.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:960 
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Reflection/ConstructorInfo.cs:22 
  at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x0001b] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
  at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00111] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
  at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x00023] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
  at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
  at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
  at AndroidX.AppCompat.View.Menu.MenuBuilder.FindItem (System.Int32 id) [0x00036] in <c5220b8e1f564a28bc774e534daebefd>:0 
  at NatureMapr.Mobile.Droid.Activities.BaseActivity.OnCreateOptionsMenu (Android.Views.IMenu menu) [0x00013] in C:\Projects\NatureMapr\NatureMapr.Mobile.Droid\Activities\BaseActivity.cs:73 
  at Android.App.Activity.n_OnCreateOptionsMenu_Landroid_view_Menu_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_menu) [0x0000f] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.8(intptr,intptr,intptr)

Any assistance on how I can track down the issue would be much appreciated.


Solution

  • For anyone else running into this error, it looks like this was a problem with one or more of the 1.0.0-preview02 Xamarin AndroidX NuGet packages. Upgrading to the 1.0.0.2 packages has fixed the problem for me.