Skip to content

Xamarin.Android TravelSample InvalidCastException on UserDialogs.Init() in MainActivity.cs #48

@bdgza

Description

@bdgza

I am running the sample app in Visual Studio for Mac 8.0.8 with Xcode 10.2.1. The NuGets have not been updated, the Xamarin Forms is 2.5.0.91635. I do not have a backend installed, so looking up a hotel in Hotel Search throws an exception and the alert dialog for "Error fetching hotels" is triggered. The initialisation for Acr.UserDialogs (6.5.1) is done using:
UserDialogs.Init(() => (Activity)ApplicationContext);
in MainActivity.cs. Instead of showing the alert dialog this fails with an InvalidCastException.

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.InvalidCastException: Specified cast is not valid.
[MonoDroid] at TravelSample.Droid.MainActivity.b__0_0 () [0x00000] in /Users/bdegroot/Downloads/mobile-travel-sample-master/dotnet/TravelSample/TravelSample.Android/MainActivity.cs:47
[MonoDroid] at Acr.UserDialogs.UserDialogsImpl.Alert (Acr.UserDialogs.AlertConfig config) [0x0000d] in C:\dev\acr\userdialogs\src\Acr.UserDialogs.Android\UserDialogsImpl.cs:34
[MonoDroid] at Acr.UserDialogs.AbstractUserDialogs.AlertAsync (Acr.UserDialogs.AlertConfig config, System.Nullable`1[T] cancelToken) [0x00054] in C:\dev\acr\userdialogs\src\Acr.UserDialogs.Interface\AbstractUserDialogs.cs:130
[MonoDroid] at TravelSample.Core.ViewModels.HotelListViewModel.LookupAsync () [0x00146] in /Users/bdegroot/Downloads/mobile-travel-sample-master/dotnet/TravelSample/TravelSample.Core/ViewModels/HotelListViewModel.cs:268
[MonoDroid] at TravelSample.Core.ViewModels.HotelListViewModel.<get_LookupCommand>b__13_0 () [0x0001f] in /Users/bdegroot/Downloads/mobile-travel-sample-master/dotnet/TravelSample/TravelSample.Core/ViewModels/HotelListViewModel.cs:221
[MonoDroid] at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) [0x00000] in <58604b4522f748968296166e317b04b4>:0
[MonoDroid] at Android.App.SyncContext+<>c__DisplayClass2_0.b__0 () [0x00000] in <788a34f7a7b84486905dfde786529d42>:0
[MonoDroid] at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <788a34f7a7b84486905dfde786529d42>:0
[MonoDroid] at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <788a34f7a7b84486905dfde786529d42>:0
[MonoDroid] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.34(intptr,intptr)

Changing the line to:
UserDialogs.Init(this);
fixes the problem (in this case).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions