Bug 31878 - AutocompletePredictionBuffer.Get() returns a ZzcInvoker rather than IAutocompletePrediction
Summary: AutocompletePredictionBuffer.Get() returns a ZzcInvoker rather than IAutocomp...
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 5.1
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-07-13 09:49 UTC by Adam Hartley [MSFT]
Modified: 2015-07-15 17:41 UTC (History)
4 users (show)

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


Attachments
Sample (1.46 MB, application/zip)
2015-07-13 09:49 UTC, Adam Hartley [MSFT]
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 ANSWERED

Description Adam Hartley [MSFT] 2015-07-13 09:49:25 UTC
Created attachment 11999 [details]
Sample

## Overview

AutocompletePredictionBuffer.Get() at index 0 returns a ZzcInvoker rather than IAutocompletePrediction.

## Steps to reproduce

1. Download sample
2. Place an API key for Google Places in the manifest

<meta-data android:name="com.google.android.geo.API_KEY" android:value="" />

3. Run on a device

## Expected result

Type should be IAutocompletePrediction

## Actual result

Type is ZzcInvoker rather than IAutocompletePrediction (see exception which catches this)

Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException
  at --- End of managed exception stack trace ---
  at java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
  at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  at Caused by: java.lang.reflect.InvocationTargetException
  at at java.lang.reflect.Method.invoke(Native Method)
  at at java.lang.reflect.Method.invoke(Method.java:372)
  at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  at ... 1 more
  at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.Exception: Type is ZzcInvoker
  at XamarinPlacesApiBug.PlaceAutocomplete.AutocompleteResult (Android.Gms.Location.Places.AutocompletePredictionBuffer) [0x0007d] in /Users/Adam/Downloads/XamarinPlacesApiBug/XamarinPlacesApiBug/PlaceAutocomplete.cs:64
  at at Android.Gms.Common.Apis.ResultCallback`1<Android.Gms.Location.Places.AutocompletePredictionBuffer>.OnResult (Java.Lang.Object) <0x000eb>
  at at Android.Gms.Common.Apis.IResultCallbackInvoker.n_OnResult_Lcom_google_android_gms_common_api_Result_ (intptr,intptr,intptr) <IL 0x00013, 0x000ff>
  at at (wrapper dynamic-method) object.706f8045-f536-4279-9234-fa3d2bd43ff2 (intptr,intptr,intptr) <IL 0x00017, 0x0004b>
  at at md526b7ac14cffc1a788e82c7b73f3add08.ResultCallback_1.n_onResult(Native Method)
  at at md526b7ac14cffc1a788e82c7b73f3add08.ResultCallback_1.onResult(ResultCallback_1.java:29)
  at at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.deliverResultCallback(Unknown Source)
  at at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.handleMessage(Unknown Source)
  at at android.os.Handler.dispatchMessage(Handler.java:102)
  at at android.os.Looper.loop(Looper.java:135)
  at at android.app.ActivityThread.main(ActivityThread.java:5254)
  at ... 4 more
Comment 1 Jonathan Pryor 2015-07-15 11:52:27 UTC
Aside: Prefix lines with '>' in order to inhibit line wrapping. This makes things like stack traces readable:

> Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException
>   at --- End of managed exception stack trace ---
>   at java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
>   at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
>   at Caused by: java.lang.reflect.InvocationTargetException
>   at at java.lang.reflect.Method.invoke(Native Method)
>   at at java.lang.reflect.Method.invoke(Method.java:372)
>   at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
>   at ... 1 more
>   at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.Exception: Type is ZzcInvoker
>   at XamarinPlacesApiBug.PlaceAutocomplete.AutocompleteResult (Android.Gms.Location.Places.AutocompletePredictionBuffer) [0x0007d] in /Users/Adam/Downloads/XamarinPlacesApiBug/XamarinPlacesApiBug/PlaceAutocomplete.cs:64
>   at at Android.Gms.Common.Apis.ResultCallback`1<Android.Gms.Location.Places.AutocompletePredictionBuffer>.OnResult (Java.Lang.Object) <0x000eb>
>   at at Android.Gms.Common.Apis.IResultCallbackInvoker.n_OnResult_Lcom_google_android_gms_common_api_Result_ (intptr,intptr,intptr) <IL 0x00013, 0x000ff>
>   at at (wrapper dynamic-method) object.706f8045-f536-4279-9234-fa3d2bd43ff2 (intptr,intptr,intptr) <IL 0x00017, 0x0004b>
>   at at md526b7ac14cffc1a788e82c7b73f3add08.ResultCallback_1.n_onResult(Native Method)
>   at at md526b7ac14cffc1a788e82c7b73f3add08.ResultCallback_1.onResult(ResultCallback_1.java:29)
>   at at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.deliverResultCallback(Unknown Source)
>   at at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.handleMessage(Unknown Source)
>   at at android.os.Handler.dispatchMessage(Handler.java:102)
>   at at android.os.Looper.loop(Looper.java:135)
>   at at android.app.ActivityThread.main(ActivityThread.java:5254)
>   at ... 4 more

> Type should be IAutocompletePrediction

This isn't possible; IAutocompletePrediction is an interface, so it can't be of that type. It would need to be of a type that implements IAutocompletePrediction.

That said, your test uses `is`, and it's that check which is failing.

Unfortunately, Attachment #11999 [details] doesn't fail for me; it starts, shows a Toast message that says "PLACES_API_QUOTA_FAILED", then nothing.

What I would like you to do is change PlaceAutocomplete.cs:64 from:

> throw new Exception( "wtf is " + item.GetType().Name );

to:

> Console.WriteLine ("# jonp: EXPECTED IAutocompletePrediction; got: {0}", item.GetType ().FullName);
> Console.WriteLine ("# jonp:                              JNI type: {0}", JNIEnv.GetClassNameFromInstance (item.Handle));

Then provide the `adb logcat` output with those messages.

Regardless, the actual problem is *probably* this: when a Java instance enters managed code and there isn't a pre-existing wrapper for it, Xamarin.Android will construct one. To determine the wrapper type, we walk the Java-side inheritance list (classes, *not* interfaces!) to see if any of the base classes have been bound.

In this particular case, the instance you have is apparently a subclass of android.gms.common.apis.zzc or android.gms.common.data.zzc, so that's the most derived wrapper that we can create. Because these types are abstract, in order to create a wrapper we need to use an "Invoker" type, hence "ZzcInvoker" as the Name that your message generates.

Assuming this is the case, this is (more or less) By Design. The workaround is to use the JavaCast<T>() extension method:

-                if( item is IAutocompletePrediction ) {
-                    a.Add( (IAutocompletePrediction) item );
+                var item = buffer.Get( i ).JavaCast<IAutocompletePrediction>();
+                a.Add( item );
Comment 2 Adam Hartley [MSFT] 2015-07-15 16:39:45 UTC
@Jonathan Thanks for the info regarding the line wrapping. Also, to run the sample you need to specify an API key (as noted in step 2). I can send one over if you still want to have a look at this.