Bug 7459 - Casting problem when using Android.Support.V4.View namespace
Summary: Casting problem when using Android.Support.V4.View namespace
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries (show other bugs)
Version: 4.2.x
Hardware: PC Windows
: Normal normal
Target Milestone: 6.0 (C6)
Assignee: Bugzilla
URL:
: 13791 17074 25562 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-09-25 19:27 UTC by Ales Suchac
Modified: 2015-06-09 10:51 UTC (History)
11 users (show)

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


Attachments
Simple project to demonstrate problem with casting to Android.Support.V4.View.ViewPager (1.43 MB, application/zip)
2012-09-25 19:27 UTC, Ales Suchac
Details

Description Ales Suchac 2012-09-25 19:27:34 UTC
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.


Actual Results:
Runtime error.

Expected Results:
No error.


How often does this happen? 
Always.


Additional Information:
Jonathan Pryor from Xamarin support offered 2 workarounds:

1:
pager = v.JavaCast<Android.Support.V4.View.ViewPager>();

2:
pager = FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.ph_hlp_pager);
Comment 1 Atsushi Eno 2012-09-27 09:53:55 UTC
internal note: This assembly needs to be registered as non-framework assembly.
Comment 3 Jonathan Pryor 2012-11-02 13:15:21 UTC
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).

Ew. :-/
Comment 5 dean.ellis 2012-11-06 13:41:24 UTC
Fixed in master/7a15ce57e3
Comment 6 Jonathan Pryor 2012-11-06 14:01:49 UTC
Note: master/7a15ce57e3 fixes Comment #1, not the original bug.
Comment 8 Jonathan Pryor 2013-08-07 15:14:51 UTC
*** Bug 13791 has been marked as a duplicate of this bug. ***
Comment 9 Atsushi Eno 2014-01-08 05:40:04 UTC
*** Bug 17074 has been marked as a duplicate of this bug. ***
Comment 10 Jonathan Pryor 2015-02-06 22:38:39 UTC
*** Bug 25562 has been marked as a duplicate of this bug. ***
Comment 11 Jonathan Pryor 2015-04-22 18:01:32 UTC
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.
Comment 12 Ram Chandra 2015-06-09 10:51:10 UTC
I have checked this issue and I am able to reproduce this issue with XS: 5.8.3.1 and XA: 4.2..0
Screencast: http://www.screencast.com/t/HV01pCZRW

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
Runtime:
 Microsoft .NET 4.0.30319.34011
 GTK+ 2.24.22 (MS-Windows theme)
 GTK# 2.12.26
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.

Screencast: http://www.screencast.com/t/c0wDgKEMZnc

This issue has been fixed. Hence, I am closing this issue.

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