MvvmCross: App crash with missing constructor on MvxImageView
It seems that the optional parameter added to some/most of the MvxImageView constructors will make the app crash when Activator.CreateInstance is called by MvxAndroidViewFactory due to it not being able to find the proper constructor on MvxImageView.
The constructors changed in this commit: https://github.com/MvvmCross/MvvmCross/commit/0b36189d8711d3bc5d666e6e6817c21bb6b36007#diff-f589bfb4a2af223a198d622ddfbb955b
This SO thread has some good information on Reflection & optional parameters: https://stackoverflow.com/questions/2421994/invoking-methods-with-optional-parameters-through-reflection Also this SO thread has some more specific information about Activator.CreateInstance: https://stackoverflow.com/questions/11002523/activator-createinstance-with-optional-parameters
Imho, there’s 2 ways to fix this, either change the mvximageview constructors (get rid of optional parametes) or change the way the instance is created in the MvxAndroidViewFactory.
Steps to reproduce
- Use the InternetMinute sample, use package version in the sample (4.0.0-beta2) or upgrade to 4.4.0
- Build & verify that the app starts ok
- Upgrade the mvvmcross packages to 5.0.1
- Clean the solution (important to do this), build & start the app, it crashes on load of the HomeView
Expected behavior
HomeView loads
Actual behavior
App crashes with unhandled exception:
06-05 19:18:43.712 I/MvxBind ( 7148): 1.49 Exception during creation of Mvx.MvxImageView from type MvvmCross.Binding.Droid.Views.MvxImageView - exception MissingMethodException: Constructor on type 'MvvmCross.Binding.Droid.Views.MvxImageView' not found.
06-05 19:18:43.712 I/MvxBind ( 7148): at System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) [0x00213] in <3fd174ff54b146228c505f23cf75ce71>:0
06-05 19:18:43.712 I/MvxBind ( 7148): at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x000a1] in <3fd174ff54b146228c505f23cf75ce71>:0
06-05 19:18:43.712 I/MvxBind ( 7148): at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
06-05 19:18:43.712 I/MvxBind ( 7148): at MvvmCross.Binding.Droid.Binders.MvxAndroidViewFactory.CreateView (Android.Views.View parent, System.String name, Android.Content.Context context, Android.Util.IAttributeSet attrs) [0x00019] in D:\git\MvvmCross2\MvvmCross\Binding\Droid\Binders\MvxAndroidViewFactory.cs:42
[0:] MvxBind:Error: 1.49 Exception during creation of Mvx.MvxImageView from type MvvmCross.Binding.Droid.Views.MvxImageView - exception MissingMethodException: Constructor on type 'MvvmCross.Binding.Droid.Views.MvxImageView' not found.
at System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) [0x00213] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x000a1] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at MvvmCross.Binding.Droid.Binders.MvxAndroidViewFactory.CreateView (Android.Views.View parent, System.String name, Android.Content.Context context, Android.Util.IAttributeSet attrs) [0x00019] in D:\git\MvvmCross2\MvvmCross\Binding\Droid\Binders\MvxAndroidViewFactory.cs:42
Configuration
Version: 5.0.1
Platform:
- iOS
- Android
- WPF
- UWP
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (6 by maintainers)
I don’t think it will, as the calling code:
var view = Activator.CreateInstance(viewType, context, attrs) as View;Will search for a constructor that matches something like:
public MvxImageView(Context context, IAttributeSet attrs)This constructor does not exist, due to the optional parameter. A quick fix would probably be to change this constructor:
To these 2:
Whilst I’m personally not a fan of optional constructor parameters, I think the better fix would be to change the calling code that instantiates the object to pick the correct constructor.