How to avoid TargetInvocationException when calling constructor of custom view which loaded dynamically by Xamarin.Android Application comiled by VS 2019/Xamarin.

啓一 木暮 1 Reputation point
2022-03-30T06:08:49.77+00:00

On Visual Studio 2017/Xamarin.Android, it is possible to create custom view in class library assembly by executing Activator.Activator.CreateInstance method. But On Visual Studio 2019/Xamarin.Android, TargetInvocationException (no java peer type found) are thrown.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NotSupportedException: Cannot create instance of type 'ClassLibrary.ButtonEx': no Java peer type found.
at Java.Interop.JniPeerMembers+JniInstanceMethods..ctor (System.Type declaringType)
at Java.Interop.JniPeerMembers+JniInstanceMethods.GetConstructorsForType (System.Type declaringType)
at Java.Interop.JniPeerMembers+JniInstanceMethods.StartCreateInstance (System.String constructorSignature, System.Type declaringType, Java.Interop.JniArgumentValue* parameters)
at Android.Widget.Button..ctor (Android.Content.Context context)
at ClassLibrary.ButtonEx..ctor (Android.Content.Context context)

Details: 1655173

So, I need help to avoid TargetInvocationException in this case.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.