7
votes

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.Func1<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.MvxValueEventArgs1) 03-16 03:53:04.110 I/MonoDroid( 593): at (wrapper delegate-invoke) System.EventHandler1<Cirrious.CrossCore.Core.MvxValueEventArgs1>.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.EventHandler1>,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).

2
What do you have in trace output?chamamo
I have just updated the questions and included complete outputAlexey Strakh

2 Answers

6
votes

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:

  • try looking at the trace or log output - including routing this output to something that works in Release mode.
  • try adding an exception handler around your base.OnCreate() call in MainView to see if it explains the problem.
  • try adding some trace and/or breakpoints to your MainViewModel constructor - does it get called?
  • try searching here for other problems which happen only in Release in Xamarin and/or MvvmCross
1
votes

I've faced same problem but tip from Stuart helped to figure it out.

I have ContactsViewModel which uses ContactsService via IoC engine. My MainViewModel contains ContactsViewModel ('cause Im using tab bar, like in tutorial).

And I just tried to remove service call inside ContactsViewModel and everything is fine. I've catched the error inside ctor and it said "The operation couldn’t be completed. (ABAddressBookErrorDomain error 1.)", so it's my service's fault.

So if anybody hit this error, just know remember that confusing "TargetInvocationException" is thrown always when any exception occur in ctor (particularly).