Bug 43692 - Receiving ClassCastException exception when using PushModalAsync for a page wrapped in a NavigationPage
Summary: Receiving ClassCastException exception when using PushModalAsync for a page w...
Alias: None
Product: Runtime
Classification: Mono
Component: Interop (show other bugs)
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
: 42579 (view as bug list)
Depends on:
Reported: 2016-08-24 06:35 UTC by Scott Simms
Modified: 2017-08-30 15:38 UTC (History)
16 users (show)

See Also:
Tags: Android ac
Is this bug a regression?: ---
Last known good build:

Repro for Modal Issue (73.46 KB, application/x-zip-compressed)
2016-08-24 06:35 UTC, Scott Simms
Repro for Modal issue (custom renderer) (120.97 KB, application/x-zip-compressed)
2016-08-24 06:36 UTC, Scott Simms

Description Scott Simms 2016-08-24 06:35:27 UTC
Created attachment 17191 [details]
Repro for Modal Issue

Receiving an exception when using PushModalAsync for a page wrapped in a NavigationPage on Android. This is occurring when navigating from a page containing a Google map.

The exception is:  "Java.Lang.ClassCastException: android.widget.ImageView cannot be cast to android.view.ViewGroup"

I have seen two difference behaviours; 1) the exception is raised on PushModalAsync, 2) the modal appears with blank contents and the exception is raised on the second PushModalAsync.

Two reproductions have been created - one with a custom renderer and one with Xamarin Forms Maps (attached). The issue has been reproduced on Android 4.4, 5.1 and 6.

FormsAppCompatActivity is used.
Comment 1 Scott Simms 2016-08-24 06:36:54 UTC
Created attachment 17192 [details]
Repro for Modal issue (custom renderer)
Comment 2 Florin Chelaru 2016-09-07 16:36:13 UTC
I am getting the same error. Any progress on this?
Comment 3 Cliff Cawley 2016-09-07 21:18:37 UTC
*** Bug 42579 has been marked as a duplicate of this bug. ***
Comment 4 Daler 2016-10-04 01:33:03 UTC
I'm having same error. Any workaround maybe?
Comment 5 Florin Chelaru 2016-10-10 23:48:13 UTC
Unhappy workaround I'm using: on the parent page, before calling PushModalAsync, I set mapContainer.Content = null; (mapContainer is the ContentView that encapsulates the map). When the modal is popped, I set mapContainer.Content = map. Unfortunately, this causes the map renderer to be disposed, so the map is reset. I store the map state separately.

Let me know if this works for you guys or if you were able to come up with something better.
Comment 6 Scott Simms 2016-10-14 02:01:03 UTC
I ended up creating a toolbar control (title, toolbar button etc) to mimic the NavigationPage toolbar and just not wrapping the page in a NavigationPage...

Also tried removing the map as you did but not convinced that this can be made stable in all scenarios.
Comment 7 Marlon Ribeiro 2016-10-20 19:10:43 UTC
Scott, I tried your workaroud and that is ok, the MyModalPage is not blank anymore. 
But there is a problem: in my case, I'm pushing a NavigationPage(MyModalPage). Then, when I push any page on this NavigationPage and Pop it, I receive this same exception again.
Does anybody have another workaround for this?
Comment 8 Alexandre Izefler 2016-10-26 19:27:59 UTC
Marlon and Scott, I have same problem, found a solution to solve the problem?
Comment 9 Scott Simms 2016-10-26 21:18:29 UTC
Hi Alexandre,

I have not found a solution for this issue. The workaround was to drop the use of NavigationPage with modal dialogs and use a custom toolbar.

Also, I haven't retested this on the latest pre-release so that might be worth trying.

Comment 10 Cliff Cawley 2016-10-27 06:59:15 UTC
My workaround for this is to also not use modal dialogs but instead of a custom toolbar, I use the NavigationPage and just hide the back button and block the hardware back button on Android.

Works very well and I don't need a custom toolbar.

I.e. NavigationPage.SetHasBackButton(this, false);
Comment 11 Marlon Ribeiro 2016-11-01 18:19:22 UTC
Cliff, what do you mean when you say "block the hardware button"?
I didn't know it's possible... can you post your code here?
Comment 12 Marlon Ribeiro 2016-11-01 18:31:26 UTC
Ohhh I just override the OnBackButtonPressed and it works! For some reason I thought it only works on ModalPage... so it's working here. I will try to avoid modal pages until Xamarin fix it.
Comment 13 Hiran Peiris 2016-12-22 05:18:39 UTC
I am also getting the same error. 
Xamarin Forms 2.3.*
Xamarin team have't provided any solution so far... :(
Comment 14 JuanSe 2017-01-09 17:08:46 UTC
Also getting the same error.
Comment 15 Chris King 2017-05-05 19:45:50 UTC
VS Debugger puts no XF frames on the stack. Passing the buck...

 	0xFFFFFFFFFFFFFFFF in System.Diagnostics.Debugger.Mono_UnhandledException_internal	C#
 	0x1 in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/corlib/System.Diagnostics/Debugger.cs:122,4	C#
 	0x6 in Android.Runtime.UncaughtExceptionHandler.UncaughtException at /Users/builder/data/lanes/4468/b16fb820/source/xamarin-android/src/Mono.Android/Android.Runtime/UncaughtExceptionHandler.cs:43,4	C#
 	0x1C in Java.Lang.Thread.IUncaughtExceptionHandlerInvoker.n_UncaughtException_Ljava_lang_Thread_Ljava_lang_Throwable_ at /Users/builder/data/lanes/4468/b16fb820/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Java.Lang.Thread.cs:226,5	C#
 	0x1D in object.d6057ee3-d2dd-4858-b077-cc7759738351	C#
Comment 16 clay.zuvich 2017-08-30 15:38:45 UTC
Same error here.  The workaround of removing parents does not work all of the time.  I still get the error.  I think Android is trying to "catch up".

Note You need to log in before you can comment on or make changes to this bug.