Bug 25562 - Cannot cast LayoutParams of RecyclerView Child to RecyclerView.LayoutParams
Summary: Cannot cast LayoutParams of RecyclerView Child to RecyclerView.LayoutParams
Status: RESOLVED DUPLICATE of bug 7459
Alias: None
Product: Android
Classification: Xamarin
Component: Bindings ()
Version: 5.0
Hardware: Macintosh Mac OS
: High normal
Target Milestone: ---
Assignee: Atsushi Eno
URL:
Depends on:
Blocks:
 
Reported: 2014-12-21 16:24 UTC by Steve Flack
Modified: 2015-02-06 22:38 UTC (History)
6 users (show)

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


Attachments
Sample project demonstrating InvalidCastException (1.09 MB, application/zip)
2015-01-23 11:52 UTC, Mark Taparauskas
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED DUPLICATE of bug 7459

Description Steve Flack 2014-12-21 16:24:49 UTC
The following code crashes at runtime:

		public void DrawVertical(Canvas c, RecyclerView parent)
		{
			int left = parent.PaddingLeft;
			int right = parent.Width - parent.PaddingRight;

			int childCount = parent.ChildCount;
			for (var i = 0; i < childCount; i++) {
				var child = parent.GetChildAt(i);
				RecyclerView.LayoutParams p = (RecyclerView.LayoutParams)child.LayoutParameters;
				var top = child.Bottom + p.BottomMargin;
				int bottom = top + mDivider.IntrinsicHeight;
				mDivider.SetBounds(left, top, right, bottom);
				mDivider.Draw(c);
			}
		}

Where the cast to RecyclerView.LayoutParams fails. Upon inspection the type of the child layout params is shown as android.support.v7.widget.RecyclerView$LayoutParams yet the cast to RecyclerView.LayoutParams fails. The above code is a port of some Java code to draw separators on list items.
Comment 1 Steve Flack 2014-12-21 16:26:23 UTC
=== Xamarin Studio ===

Version 5.7 (build 660)
Installation UUID: fbca1238-41dc-4461-9ffb-8405831de509
Runtime:
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.6.0.41 (Indie Edition)
Hash: 2c57f4e
Branch: 
Build date: 2014-12-18 16:42:49-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Indie Edition)
Android SDK: /Users/steve/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin.Mac ===

Version: 1.11.3.0 (Indie Edition)

=== Build Information ===

Release ID: 507000660
Git revision: 6a74f9bdb90d9415b597064d815c9be38b401fee
Build date: 2014-12-18 00:38:00-05
Xamarin addins: 57be0f5e1433cb58b70ea9b52e9dc77ad791be40

=== Operating System ===

Mac OS X 10.10.1
Darwin MacBook-Pro.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 2 Parmendra Kumar 2014-12-22 00:08:15 UTC
I have tried to reproduce this issue but I am not able to reproduce this issue. Could you please provide us sample project and steps that can be helpful to reproduce at my end?

Environment info:

Xamarin Studio 5.7 (build 660)
Installation UUID: 1a096c6f-0678-402e-89b2-a2c10f7e80e4
Runtime:
	Mono 3.12.0 ((detached/c82a2e5)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000070

=== Apple Developer Tools ===

Xcode 6.1 (6602)
Build 6A1052c

Xamarin.Android 

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/360_macmini/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.Mac ===

Version: 1.10.0.18 (Business Edition)


=== Operating System ===

Mac OS X 10.9.4
Darwin ShrutiMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 3 Steve Flack 2014-12-22 02:01:48 UTC
Did you check at runtime as it's fine at compile time?
Comment 4 Parmendra Kumar 2014-12-29 08:35:15 UTC
Yes Steve I have checked it at runtime but I am not able to reproduce this issue. Could you Please provide us sample project, applicationOutput Log, Ide Log so that we can reproduce it at our end.

You can get Application output Log via:  View->Pads->Application Output
Ide Log via: Help->Open Log Directory-> Ide Log
Comment 5 Mark Taparauskas 2015-01-23 11:52:19 UTC
Created attachment 9473 [details]
Sample project demonstrating InvalidCastException

The attached project throws InvalidCastException on this line in MainActivity.cs:

var lp2 = ((StaggeredGridLayoutManager.LayoutParams)(holder.ItemView.LayoutParameters)); 

I think this code should work - it is a direct port of Java code for this technique and the debugger locals window shows that the object I am trying to cast is a 			StaggeredGridLayoutManager.LayoutParams.
Comment 6 Dominic N [MSFT] 2015-02-06 12:31:00 UTC
I am changing the status on this bug to confirmed.

Using the above sample project and the following steps:

1. Download sample app
2. Open in Xamarin Studio on Mac
3. Deploy to Android emulator

 I have been able to replicate the InvalidCastException error with the following setup:

=== Xamarin Studio ===

Version 5.7 (build 661)
Installation UUID: ee07b9c8-41e3-496f-a1ab-e8a3ed3db20f
Runtime:
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.6.0.51 (Business Edition)
Hash: dfb682f
Branch: 
Build date: 2015-01-08 13:39:32-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/<redacted>/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin.Mac ===

Version: 1.10.0.18 (Business Edition)

=== Build Information ===

Release ID: 507000661
Git revision: b70bab61da996da29045ea8ee8aed1a6faedbe78
Build date: 2015-01-05 16:31:31-05
Xamarin addins: 82f6c71490562d6cd125a09287f441902fdac3d7

=== Operating System ===

Mac OS X 10.9.5
Darwin MacBook-Pro.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 7 Atsushi Eno 2015-02-06 15:33:33 UTC
This is bug in app code. The corresponding code should use JavaCast<>() like:

var lp2 = holder.ItemView.LayoutParameters.JavaCast<StaggeredGridLayoutManager.LayoutParams>();
Comment 8 René 2015-02-06 15:48:07 UTC
Thanks Atsushi - but can you explain why this is necessary and what is going on there? Why does a normal cast not work?
Comment 9 Atsushi Eno 2015-02-06 16:21:34 UTC
Because the .NET type system is different from Java type system. Check out the API documentation for JavaCast method here: http://androidapi.xamarin.com/?link=M%3aAndroid.Runtime.Extensions.JavaCast%3CTResult%3E
Comment 10 Jonathan Pryor 2015-02-06 22:38:39 UTC
It's not *exactly* an app bug, it's a bug/limitation in how Xamarin.Android performs bindings, and it's something that we could do better on; see Bug #7459.

That said, while fixing Bug #7459 would solve this issue, it wouldn't be a "general" solution, because when we create a wrapper around a Java instance, we only consider the class hierarchy, and not any implemented interfaces, so there are some scenarios such as casting EGLContext.EGL to an IEGL10 interface which will likely always require a JavaCast<T>().

*** This bug has been marked as a duplicate of bug 7459 ***