Forum Xamarin Xamarin.Forms

System.Reflection.TargetInvocationException when calling a DisplayAlert

RicardoSRicardoS ESMember ✭✭✭
edited January 16 in Xamarin.Forms

Yep, a new one. And this time this happens to ANY project.

Whenever a call to DisplayAlertS is made, we get this exception:

System.Reflection.TargetInvocationException

Exception has been thrown by the target of an invocation.

at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00081] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:409
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/Reflection/MethodBase.cs:53
at Xamarin.Forms.MessagingCenter+Subscription.InvokeCallback (System.Object sender, System.Object args) [0x00064] in D :\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:94
at Xamarin.Forms.MessagingCenter.InnerSend (System.String message, System.Type senderType, System.Type argType, System.Object sender, System.Object args) [0x0006b] in D :\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:217
at Xamarin.Forms.MessagingCenter.Xamarin.Forms.IMessagingCenter.Send[TSender,TArgs] (TSender sender, System.String message, TArgs args) [0x00013] in D :\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:115
at Xamarin.Forms.MessagingCenter.Send[TSender,TArgs] (TSender sender, System.String message, TArgs args) [0x00000] in D :\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:108
at Xamarin.Forms.Page.DisplayAlert (System.String title, System.String message, System.String accept, System.String cancel) [0x00039] in D :\a\1\s\Xamarin.Forms.Core\Page.cs:205
at Xamarin.Forms.Page.DisplayAlert (System.String title, System.String message, System.String cancel) [0x00000] in D :\a\1\s\Xamarin.Forms.Core\Page.cs:195
at RegularesMarina.MainPage.GoFave (System.Object sender, System.EventArgs e) [0x0003e] in /Users/macbookpro/proyectos/RegularesMarina/RegularesMarina/Pages/MainPage.xaml.cs:125
at Xamarin.Forms.Button.Xamarin.Forms.Internals.IButtonElement.PropagateUpClicked () [0x00000] in D :\a\1\s\Xamarin.Forms.Core\Button.cs:185
at Xamarin.Forms.ButtonElement.ElementClicked (Xamarin.Forms.VisualElement visualElement, Xamarin.Forms.Internals.IButtonElement ButtonElementManager) [0x0001f] in D :\a\1\s\Xamarin.Forms.Core\ButtonElement.cs:61
at Xamarin.Forms.Button.SendClicked () [0x00000] in D :\a\1\s\Xamarin.Forms.Core\Button.cs:171
at Xamarin.Forms.Platform.Android.ButtonRenderer+ButtonClickListener.OnClick (Android.Views.View v) [0x0000f] in D :\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ButtonRenderer.cs:325
at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View
(System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x00011] in :0
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.33(intptr,intptr,intptr)

Soo, yeah. we need to fix this A.S.A.P.

Oh, and doing the

Device.BeginInvokeOnMainThread (new Action (async () =>
                {
                    await DisplayAlert ("Title", "Message", "Ok");
                }));

trick does not work.

Please and thank you.

Best Answer

  • RicardoSRicardoS ESMember ✭✭✭
    Accepted Answer

    And solved.

    The issue was in Xamarin.Forms. An incompatibility with FormsApplicationActivity and legacy activities.

    Thank you everyone (@ColeX, @Dinesh_Official) for the help provided and have a nice day.

    Manuel R.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 16

    If you don't need to get callback from the alert , just remove await , modify your code as

               Device.BeginInvokeOnMainThread(() =>
               {
                   DisplayAlert("Title", "Message", "Ok");
               });
    

    Another option : use App.Current.MainPage.DisplayAlert(...); , since maybe you call the method outside of the page or view .

    Refer https://stackoverflow.com/a/47588584/8187800 .

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX said:
    If you don't need to get callback from the alert , just remove await , modify your code as

               Device.BeginInvokeOnMainThread(() =>
               {
                   DisplayAlert("Title", "Message", "Ok");
               });
    

    Another option : use App.Current.MainPage.DisplayAlert(...); , since maybe you call the method outside of the page or view .

    Refer https://stackoverflow.com/a/47588584/8187800 .

    Nope, none of them have worked. Same exception.

  • Dinesh_OfficialDinesh_Official Member ✭✭✭

    @RicardoS ,

    Can you share the code snippet in your button event?

    As per the below line,
    https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/Page.cs#L205

    If you are displaying the alert box within the page, it will work, Else if you are doing any navigation code in the button, it might have possibility to throw the exception.

  • ColeXColeX Member, Xamarin Team Xamurai

    @RicardoS said:

    @ColeX said:
    If you don't need to get callback from the alert , just remove await , modify your code as

               Device.BeginInvokeOnMainThread(() =>
               {
                   DisplayAlert("Title", "Message", "Ok");
               });
    

    Another option : use App.Current.MainPage.DisplayAlert(...); , since maybe you call the method outside of the page or view .

    Refer https://stackoverflow.com/a/47588584/8187800 .

    Nope, none of them have worked. Same exception.

    What't the version of your Xamarin.Forms ? I need to reproduce the error .

    It's better to provide a basic project here , just leave the core code and upload on the forum .

  • RicardoSRicardoS ESMember ✭✭✭

    @Dinesh_Official said:
    @RicardoS ,

    Can you share the code snippet in your button event?

    As per the below line,
    https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/Page.cs#L205

    If you are displaying the alert box within the page, it will work, Else if you are doing any navigation code in the button, it might have possibility to throw the exception.

    void GoFave (object sender, EventArgs e) {
                List<GeoHoraData> favoritos = Textos.GetFavoritos (Settings.FavouriteList);
                if (favoritos != null && favoritos.Any ()) {
                    cancel = new CancellationToken (true);
                    Navigation.PushAsync (new FavsPage ());
                } else {
                    Device.BeginInvokeOnMainThread (() => {
                        Application.Current.MainPage.DisplayAlert ("Aviso", Textos.AvisoPredef (3, 6, false, string.Empty), "Aceptar");
                    });
                }
            }
    

    I believe this is all you need?

    Tell me of anything i might have missed

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX said:

    @RicardoS said:

    @ColeX said:
    If you don't need to get callback from the alert , just remove await , modify your code as

               Device.BeginInvokeOnMainThread(() =>
               {
                   DisplayAlert("Title", "Message", "Ok");
               });
    

    Another option : use App.Current.MainPage.DisplayAlert(...); , since maybe you call the method outside of the page or view .

    Refer https://stackoverflow.com/a/47588584/8187800 .

    Nope, none of them have worked. Same exception.

    What't the version of your Xamarin.Forms ? I need to reproduce the error .

    It's better to provide a basic project here , just leave the core code and upload on the forum .

    Xamarin.Forms is at its latest: 4.4.0.991477

    I'll try to reproduce it in a new project.
    The funny thing is this used to work everywhere, and then it started to gradually fail, from one solution to another.
    This is the newest project in which happened.

    So, maybe when making a sample solution it works. But in later days it fails.

    Regards.

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX
    The sample works fine, but I noticed that Xamarin.Forms starts on the previous version: 4.3.0.908675.

    I'm updating as I type and I'll try later this evening.

    Regards

  • RicardoSRicardoS ESMember ✭✭✭

    @RicardoS said:
    @ColeX
    The sample works fine, but I noticed that Xamarin.Forms starts on the previous version: 4.3.0.908675.

    I'm updating as I type and I'll try later this evening.

    Regards

    And it worked perfectly. Hooray!

  • Dinesh_OfficialDinesh_Official Member ✭✭✭
    edited January 16

    Great

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 16

    @RicardoS said:

    @RicardoS said:
    @ColeX
    The sample works fine, but I noticed that Xamarin.Forms starts on the previous version: 4.3.0.908675.

    I'm updating as I type and I'll try later this evening.

    Regards

    And it worked perfectly. Hooray!

    I tried on Android simulator with Xamarin 4.4.0.991477 , it worked as expected.

  • RicardoSRicardoS ESMember ✭✭✭
    edited January 16

    @ColeX said:

    @RicardoS said:

    And it worked perfectly. Hooray!

    I tried on Android simulator with Xamarin 4.4.0.991477 , it worked as expected.

    Yeah, we don't have emulators here. A Samsung Galaxy Tab, I think Lollipop.

    Also, that 'Hooray!' was sarcasm. It's still failing on our projects...

  • ColeXColeX Member, Xamarin Team Xamurai

    Please raise your issue here for better help: https://github.com/xamarin/Xamarin.Forms/issues.

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX said:
    Please raise your issue here for better help: https://github.com/xamarin/Xamarin.Forms/issues.

    How do I do that? I mean, if I cannot reproduce the issue in a new project, what credentials do I have to support my complaints on Xamarin.Forms?

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX , @Dinesh_Official
    Good news, everyone! (That means bad news)

    I've pinpointed the origin of the issue.

    If you use the Android Theme "@android:style/Theme.Holo.Light.DarkActionBar" and the MainActivity (Xamarin.Forms.Android) is a FormsApplicationActivity, then it happens.

    This is a problem, since the other option is FormsAppCompatActivity, but the Holo theme does not work with that one and the "@style/MainTheme" is, emm, a bit unpleasant.

    Soo, we have the sample. How do we upload it and where?

    Best regards to all.

    Manuel R.

  • ColeXColeX Member, Xamarin Team Xamurai

    Open a new issue here, attach the detailed description and the reproduction .

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX said:
    Open a new issue here, attach the detailed description and the reproduction .

    Thank you, working on it.

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX
    How do I submit the sample project for others to try?
    I'm terrible explaining with words how the issue appears and whatnot...

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX
    Nevermind I've already figured it out...

  • RicardoSRicardoS ESMember ✭✭✭

    Oops, I almost forgot. Here is the Sample:

    https://github.com/softourS/sampleDisplay

    and this is the issue report:

    https://github.com/xamarin/Xamarin.Forms/issues/9240

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 17

    @RicardoS said:
    Oops, I almost forgot. Here is the Sample:

    https://github.com/softourS/sampleDisplay

    and this is the issue report:

    https://github.com/xamarin/Xamarin.Forms/issues/9240

    Great, let's wait for response .

  • RicardoSRicardoS ESMember ✭✭✭

    The issue was duplicated so they closed my post and move it (I think) to here:

    https://github.com/xamarin/Xamarin.Forms/issues/9214

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 20

    As the comment mentioned , are you using FormsApplicationActivity instead of FormsAppCompatActivity in Android project ?

    Since the error always happens on legacy activities.

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX said:
    As the comment mentioned , are you using FormsApplicationActivity instead of FormsAppCompatActivity in Android project ?

    Yes, we explained it on the bug report and in the sample.
    We need that top of activity in other to use HoloLight as a theme.

  • ColeXColeX Member, Xamarin Team Xamurai

    So this is the issue of FormsApplicationActivity , ask for solution in that thread if you persist to use FormsApplicationActivity .

  • RicardoSRicardoS ESMember ✭✭✭

    @ColeX said:
    So this is the issue of FormsApplicationActivity , ask for solution in that thread if you persist to use FormsApplicationActivity .

    We will.

    Thank you.

  • RicardoSRicardoS ESMember ✭✭✭
    edited January 23

    Updating Xamarin.Forms to the newest version (from 4.4.0.991477 to 4.4.0.991537), which we hope solves the bug.

  • RicardoSRicardoS ESMember ✭✭✭
    Accepted Answer

    And solved.

    The issue was in Xamarin.Forms. An incompatibility with FormsApplicationActivity and legacy activities.

    Thank you everyone (@ColeX, @Dinesh_Official) for the help provided and have a nice day.

    Manuel R.

Sign In or Register to comment.