I'm developing Xamarin.Android application with MvvmCross. Every time I start app under RELEASE configuration it crashes on start with the following error:
Cirrious.CrossCore.Exceptions.MvxException: Failed to construct and initialize ViewModel for type DroidApp.Core.ViewModels.MainViewModel from locator MvxDefaultViewModelLocator - check MvxTrace for more information
This app works absolutely fine under DEBUG configuration. The issue begins to raise just after I updated my code to use MvvmCross 3.1
Full output with stacktrace is below:
03-16 03:53:04.110 I/MonoDroid( 593): UNHANDLED EXCEPTION: Cirrious.CrossCore.Exceptions.MvxException: Failed to construct and initialize ViewModel for type Pcl.Core.ViewModels.MainViewModel from locator MvxDefaultViewModelLocator - check MvxTrace for more information 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.IMvxViewModelLocator) 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest,Cirrious.MvvmCross.ViewModels.IMvxBundle) 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.ViewModelFromRequest (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest,Cirrious.MvvmCross.ViewModels.IMvxBundle) 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.CreateViewModelFromIntent (Android.Content.Intent,Cirrious.MvvmCross.ViewModels.IMvxBundle) 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent,Cirrious.MvvmCross.ViewModels.IMvxBundle,System.Type) 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.LoadViewModel (Cirrious.MvvmCross.Droid.Views.IMvxAndroidView,Cirrious.MvvmCross.ViewModels.IMvxBundle) 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions/<>c_DisplayClass3.b_1 () 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (Cirrious.MvvmCross.Views.IMvxView,System.Func
1<Cirrious.MvvmCross.ViewModels.IMvxViewModel>) <IL 0x00013, 0x00113> 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (Cirrious.MvvmCross.Droid.Views.IMvxAndroidView,Android.OS.Bundle) <IL 0x00073, 0x0031f> 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (object,Cirrious.CrossCore.Core.MvxValueEventArgs
1) 03-16 03:53:04.110 I/MonoDroid( 593): at (wrapper delegate-invoke) System.EventHandler1<Cirrious.CrossCore.Core.MvxValueEventArgs
1>.invoke_void_this__object_TEventArgs (object,Cirrious.CrossCore.Core.MvxValueEventArgs1<Android.OS.Bundle>) <0x00067> 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.CrossCore.Core.MvxDelegateExtensionMethods.Raise<Android.OS.Bundle> (System.EventHandler
1>,object,Android.OS.Bundle) <0x000c3> 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.CrossCore.Droid.Views.MvxEventSourceActivity.OnCreate (Android.OS.Bundle) 03-16 03:53:04.110 I/MonoDroid( 593): at MyApp.Droid.Common.MvxActivityBase.OnCreate (Android.OS.Bundle) 03-16 03:53:04.110 I/MonoDroid( 593): at MyApp.Droid.Views.MainView.OnCreate (Android.OS.Bundle) 03-16 03:53:04.110 I/MonoDroid( 593): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) 03-16 03:53:04.110 I/MonoDroid( 593): at (wrapper dynamic-method) object.e62191de-ee25-45fe-b902-796a741820a1 (intptr,intptr,intptr) Unhandled Exception:Cirrious.CrossCore.Exceptions.MvxException: Failed to construct and initialize ViewModel for type Pcl.Core.ViewModels.MainViewModel from locator MvxDefaultViewModelLocator - check MvxTrace for more information 03-16 03:53:07.080 E/mono-rt ( 593): [ERROR] FATAL UNHANDLED EXCEPTION: Cirrious.CrossCore.Exceptions.MvxException: Failed to construct and initialize ViewModel for type Pcl.Core.ViewModels.MainViewModel from locator MvxDefaultViewModelLocator - check MvxTrace for more information 03-16 03:53:07.080 E/mono-rt ( 593): at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState, IMvxViewModelLocator viewModelLocator) [0x00000] in :0 03-16 03:53:07.080 E/mono-rt ( 593): at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState) [0x00000] in :0 03-16 03:53:07.080 E/mono-rt ( 593): at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.ViewModelFromRequest (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest viewModelRequest, IMvxBundle savedState) [0x00000] in :0 03-16 03:53:07.080 E/mono-rt ( 593): at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.CreateViewModelFromIntent (Android.Content.Intent intent, IMvxBundle savedState) [0x00000] The program 'Mono' has exited with code 0 (0x0).
From the information provided, I have no idea what is causing this problem.
The exception you've listed is thrown on https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross/ViewModels/MvxViewModelLoader.cs#L46
This can only occur if MvxDefaultViewModelLocator
returns false
.
When MvxDefaultViewModelLocator
returns false
it always logs a message explaining why - see
https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross/ViewModels/MvxDefaultViewModelLocator.cs#L31
To debug it I would:
base.OnCreate()
call in MainView
to see if it explains the problem.MainViewModel
constructor - does it get called?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With