Bug 36599 - Redlaser component [Scanning error] Specified cast is not valid.
Summary: Redlaser component [Scanning error] Specified cast is not valid.
Alias: None
Product: Components
Classification: Xamarin
Component: Xamarin Components ()
Version: Production (addons.xamarin.com)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Adlair Cerecedo-Mendez
Depends on:
Reported: 2015-12-04 15:49 UTC by David
Modified: 2016-02-08 22:22 UTC (History)
2 users (show)

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

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 for Bug 36599 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description David 2015-12-04 15:49:26 UTC
This code has worked for a long time but with the last Xamarin Studio update I get a exception on this line.
var newBarcodesFound = (Java.Util.ISet) scanStatus[Status.StatusNewFoundBarcodes];

My version of Xamarin Studio is 5.10 (build 871) and I am using version jdk1.7.0_71 of the Java SDK(JDK) 

Below is from your getting started section of the Redlaser component and will produce the exception when trying to cast to (Java.Util.ISet).

Getting Started on Android

Create a class that inherits from BarcodeScanActivity and override OnScanStatusUpdate method: 

using Ebay.RedLaser;	
protected override void OnScanStatusUpdate (IDictionary<String, Java.Lang.Object> scanStatus)
	//Called by the SDK repeatedly while scanning is happening. This method 
	//override is how your app can find out about the status of the scanning
	//session in real time.

	try {
			var newBarcodesFound = (Java.Util.ISet) scanStatus[Status.StatusNewFoundBarcodes];
			if (newBarcodesFound != null && newBarcodesFound.Size () > 0) {
				var barcode = (BarcodeResult)newBarcodesFound.ToArray () [0];
				Toast.MakeText (this, barcode.BarcodeString, ToastLength.Long).Show ();
	} catch (Exception e) {
			Log.Debug ("Scanning error", e.Message);
Comment 1 Jon Goldberger [MSFT] 2016-02-08 22:01:50 UTC
I have been able to reproduce this issue and it seems to be a regression in Xamarin.Android version and above. Works in XA

The Java.Lang.Object that scanStatus[Status.StatusNewFoundBarcodes] returns is a Java.Util.HashMap. 

I noted differences in the declarations for the Java.Lang.Object and the Java.Util.HashMap types that perhaps could be causing this? 

XA 5.1.9 (cast from Java.Lang.Object/Java.Util.HashMap to Java.Util.ISet works)

>[Register ("java/lang/Object", DoNotGenerateAcw = true)]
>public class Object : IDisposable, IJavaObject, IJavaObjectEx

>[Register ("java/util/HashMap", DoNotGenerateAcw = true)]
>public class HashMap : AbstractMap, ISerializable, ICloneable, IMap, IJavaObject, IDisposable
>[Register ("java/util/Set", "", "Java.Util.ISetInvoker")]
>public interface ISet : ICollection, IIterable, IJavaObject, IDisposable

XA and above:

[Register ("java/lang/Object", DoNotGenerateAcw = true, ApiSince = 1), DataContract]
	public class Object : IDisposable, IJavaObject, IJavaObjectEx
>[Register ("java/util/HashMap", DoNotGenerateAcw = true, ApiSince = 1), JavaTypeParameters (new string[] {
>		"K",
>		"V"
>	})]
>public class HashMap : AbstractMap, ISerializable, ICloneable, IMap, IJavaObject, IDisposable
>[Register ("java/util/Set", "", "Java.Util.ISetInvoker", ApiSince = 1), JavaTypeParameters (new string[] {
>		"E"
>	})]
>public interface ISet : ICollection, IIterable, IJavaObject, IDisposable
Comment 2 Jon Goldberger [MSFT] 2016-02-08 22:13:04 UTC
Not sure, but might this may need to be re-assigned to the XA team. If the cast worked in XA 5.1.9 and fails in XA then I would suspect the issue is in XA and  not the component?
Comment 3 Jon Goldberger [MSFT] 2016-02-08 22:22:52 UTC
Talked to Jon P. He suggests using 

>var newBarcodesFound = scanStatus [Status.StatusNewFoundBarcodes].JavaCast<Java.Util.ISet> ();

instead of:

>var newBarcodesFound = (Java.Util.ISet) scanStatus [Status.StatusNewFoundBarcodes];

This seems to work, so this seems to be just a need to update the Sample?