Bug 1114 - Android Callable Wrappers don't get constructors with array parameters.
Summary: Android Callable Wrappers don't get constructors with array parameters.
Alias: None
Product: Android
Classification: Xamarin
Component: Tools and Addins ()
Version: 1.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2011-09-28 17:11 UTC by Jonathan Pryor
Modified: 2011-11-07 21:37 UTC (History)
1 user (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 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:

Description Jonathan Pryor 2011-09-28 17:11:06 UTC

Subclassing a type with a constructor with array parameters does not result in the constructor being in the ACW.


	class BookAdapter : ArrayAdapter<string> {
		public BookAdapter (Context c, int n, string[] value)
			: base (c, n, value)

	[Activity (Label = "ScratchConstructors", MainLauncher = true)]
	public class Activity1 : Activity
		protected override void OnCreate (Bundle bundle)
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);
			var v = new BookAdapter (this, 0, new string [0]);			

Expected results: it works.

Actual results:

I/MonoDroid(15879): UNHANDLED EXCEPTION: Java.Lang.NoSuchMethodError: Exception of type 'Java.Lang.NoSuchMethodError' was thrown.
I/MonoDroid(15879): at Android.Runtime.JNIEnv.GetMethodID (intptr,string,string) <0x0007c>
I/MonoDroid(15879): at Android.Widget.ArrayAdapter`1<ScratchARE.Book>..ctor (Android.Content.Context,int,ScratchARE.Book[]) <0x0027b>
I/MonoDroid(15879): at ScratchARE.BookAdapter..ctor (Android.Content.Context,int,ScratchARE.Book[]) <0x0002b>
I/MonoDroid(15879): at ScratchARE.Activity1.OnCreate (Android.OS.Bundle) <0x000d7>
I/MonoDroid(15879): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x00057>
I/MonoDroid(15879): at (wrapper dynamic-method) object.94257c4d-4276-40bc-9574-e620070845bc (intptr,intptr,intptr) <0x00033>
I/MonoDroid(15879):   --- End of managed exception stack trace ---
I/MonoDroid(15879): java.lang.NoSuchMethodError: <init>
I/MonoDroid(15879): 	at scratchare.Activity1.n_onCreate(Native Method)
I/MonoDroid(15879): 	at scratchare.Activity1.onCreate(Activity1.java:25)
I/MonoDroid(15879): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
I/MonoDroid(15879): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
I/MonoDroid(15879): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
I/MonoDroid(15879): 	at android.app.ActivityThread.access$1500(ActivityThread.java:117)
I/MonoDroid(15879): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
I/MonoDroid(15879): 	at android.os.Handler.dispatchMessage(Handler.java:99)
I/MonoDroid(15879): 	at android.os.Looper.loop(Looper.java:130)
I/MonoDroid(15879): 	at android.app.ActivityThread.main(ActivityThread.java:3683)
I/MonoDroid(15879): 	at java.lang.reflect.Method.invokeNative(Native Method)
I/MonoDroid(15879): 	at java.lang.reflect.Method.invoke(Method.java:507)
I/MonoDroid(15879): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
I/MonoDroid(15879): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
I/MonoDroid(15879): 	at dalvik.system.NativeStart.main(Native Method)
E/mono    (15879): 
E/mono    (15879): Unhandled Exception: Java.Lang.NoSuchMethodError: Exception of type 'Java.Lang.NoSuchMethodError' was thrown.
E/mono    (15879):   at Android.Runtime.JNIEnv.GetMethodID (IntPtr kls, System.String name, System.String signature) [0x00000] in <filename unknown>:0 
E/mono    (15879):   at Android.Widget.ArrayAdapter`1[ScratchARE.Book]..ctor (Android.Content.Context context, Int32 textViewResourceId, ScratchARE.Book[] objects) [0x00000] in <filename unknown>:0 
E/mono    (15879):   at ScratchARE.BookAdapter..ctor (Android.Content.Context c, Int32 n, ScratchARE.Book[] value) [0x00000] in <filename unknown>:0 
E/mono    (15879):   at ScratchARE.Activity1.OnCreate (Android.OS.Bundle bundle) [0x00000] in <filename unknown>:0 
E/mono    (15879):   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00000] in <filename unknown>:0 
E/mono    (15879):   at (wrapper dynamic-method) object:94257c4d-4276-40bc-9574-e620070845bc (intptr,intptr,intptr)
E/mono    (15879):   --- End of managed exception stack trace ---
E/mono    (15879): java.lang.NoSuchMethodError: <init>
E/mono    (15879): 	at scratchare.Activity1.n_o

The problem is if you view e.g. obj/Debug/android//src/scratchare/BookAdapter.java there is no BookAdapter(android.content.Context, int, java.lang.Object[]) constructor.
Comment 1 Jonathan Pryor 2011-11-04 17:11:04 UTC
The reason it fails is because the .java code needs to be able to "stringify" the constructor parameters for the activation code path, e.g. from obj/Debug/android/src/.../BookAdapter.java:

	public BookAdapter (android.content.Context p0, int p1, int p2)
		super (p0, p1, p2);
		if (getClass () == BookAdapter.class)
			mono.android.TypeManager.Activate ("Scratch.Bxc1114.BookAdapter, Scratch.Bxc1114, Version=1.0.4325.27972, Culture=neutral, PublicKeyToken=null", "Android.Content.Context, Mono.Android, Version=, Culture=neutral, PublicKeyToken=null:System.Int32, mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e:System.Int32, mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", this, new java.lang.Object[] { p0, p1, p2 });

The first parameter to TypeManager.Activate() is the managed type to construct, while the second parameter is a ':'-separated list of the parameter types.

The problem is that the parameters for ArrayAdapter<T>(Context, int, T[]) can't be represented as a string; at least 'T[]' can't be represented as a string that Type.GetType() will accept. :-/
Comment 2 Jonathan Pryor 2011-11-04 17:24:35 UTC
Upon further thought, it's worse than I thought: we're looking up those parameter types on the type of the first parameter; even if we could "encode" generic type parameters, it would fail as the relevant BookAdapter constructor is BookAdapter(Context c, int n, string[] value), i.e. string[], not T[].

As such, we need to do our type lookup in the context of the most derived type...or do something else entirely.
Comment 3 Jonathan Pryor 2011-11-07 21:37:59 UTC
Fixed in commit 8c03aba6.