Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Created attachment 2615 [details]
Simple project to demonstrate problem with casting to Android.Support.V4.View.ViewPager
Description of Problem:
App crashes at runtime when trying to cast to type Android.Support.V4.View.ViewPager. Code compiles. Offending code:
v = FindViewById(Resource.Id.ph_hlp_pager);
pager = (Android.Support.V4.View.ViewPager) v; // <--- Crash:
// System.InvalidCastException: Cannot cast from source type to destination type.
Steps to reproduce the problem:
1. Please run the attached project in debug mode.
2. Click the button.
How often does this happen?
Jonathan Pryor from Xamarin support offered 2 workarounds:
pager = v.JavaCast<Android.Support.V4.View.ViewPager>();
pager = FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.ph_hlp_pager);
internal note: This assembly needs to be registered as non-framework assembly.
Looks like Comment #2 is wrong. It's correct for "fixing" Comment #1 -- which should be done for correctness/sanity anyway -- but it doesn't fix the actual problem.
The actual problem is that View.FindViewById() does a Java.Lang.Object.GetObject<View>(handle, transfer). Object.GetObject<T>() in turn sees if the instance is registered -- it isn't -- and so grabs the JNI type of the instance, which will be "android/support/v4/view/ViewPager". We then try to find a C# binding class for that JNI type, and if (when) that fails, we try the base class, recursively, until we find a binding.
The problem is that the Android.Support.V4.View.ViewPager type isn't being found, and the cause for that is that we try Type.GetType("Android.Support.V4.View.ViewPager") to load the binding type. That call fails, because that type isn't within Mono.Android.dll, it's in Mono.Android.Support.v4.dll, and the string we're using doesn't contain an assembly name.
Worse, that string _can't_ contain an assembly name, because the string is based upon the JNI name, which has no concept of assemblies (it's from Java-land).
The only solution I can think of involves attempting to load "Android.Support.V4.View.ViewPager" from _every_ assembly loaded into the current AppDomain (or something similarly egregious).
Fixed in master/7a15ce57e3
Note: master/7a15ce57e3 fixes Comment #1, not the original bug.
*** Bug 13791 has been marked as a duplicate of this bug. ***
*** Bug 17074 has been marked as a duplicate of this bug. ***
*** Bug 25562 has been marked as a duplicate of this bug. ***
Fixed in monodroid/e69b76e2 by generating a "type map" from all known Java types to all known managed types (and vice versa) at packaging time. This type map contains mappings for all assemblies that will be in the final .apk, and thus will contain information for Xamarin.Android.Support*.dll (among any and all other assemblies), thus permitting "best match" wrapper type to be found outside of Mono.Android.dll.
I have checked this issue and I am able to reproduce this issue with XS: 18.104.22.168 and XA: 4.2..0
To verify this issue I have checked this issue with following build:
Windows 6.2.9200.0 (64-bit)
Xamarin Studio: 5.10 (build 66)
Installation UUID: 8fb073b5-bde0-4ba6-aea5-4c0e80e82b3d
Microsoft .NET 4.0.30319.34011
GTK+ 2.24.22 (MS-Windows theme)
Xamarin.Android : 5.1.99 (Enterprise Edition)
Java SDK: C:\Program Files\Java\jdk1.7.0_72
java version "1.7.0_72"
Java(TM) SE Runtime Environment (build 1.7.0_72-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.72-b04, mixed mode)
=== Build Information ===
Release ID: 510000066
Git revision: 87dc5e2c45be31946e0822d8850b594ac8d487e2
Build date: 2015-06-08 11:47:36-04
Xamarin addins: 03ab8b02ad9622a28c153d145e9183b7d642eb09
Build lane: monodevelop-windows-master
Observation: To check this issue, I have added "Xamarin.Android.Support" component to the attached project and set the build action to "None" of "android-support-v4.jar". When I build and deploy the app on device and tap the button I observed that I am not getting any error/exception. Application is working fine without any exception.
This issue has been fixed. Hence, I am closing this issue.