Bug 26857 - EditText override method SetCompoundDrawables() Not Supported Exception at runtime
Summary: EditText override method SetCompoundDrawables() Not Supported Exception at ru...
Status: CLOSED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 5.1
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-02-09 05:57 UTC by Deval
Modified: 2015-09-11 08:30 UTC (History)
3 users (show)

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


Attachments
Error Log (3.25 KB, text/plain)
2015-02-09 05:57 UTC, Deval
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:
CLOSED ANSWERED

Description Deval 2015-02-09 05:57:17 UTC
Created attachment 9705 [details]
Error Log

I am using custom edit text class for right drawable click like put clear button in right side of the edittext and click on that clear the edittext.

I am getting Not Supported Exception in SetCompoundDrawables() method.

I have attached error log file please review and give the solutions of this error if possible using another method.

And below is my code of customedittext class

using System;
using Android.Widget;
using Android.Content;
using Android.Graphics;
using Android.Views;
using Android.App;
using ABCD.Android;
using Android.Util;
using Android.Graphics.Drawables;

namespace ABCD
{
	public class CustomEditText : EditText
	{
		private Drawable drawableRight;
		private Drawable drawableLeft;
		private Drawable drawableTop;
		private Drawable drawableBottom;

		int actionX, actionY;

		private Context _mContext;

		public CustomEditText (Context context)
			: base (context)
		{
			_mContext = context;
		}

		public CustomEditText (Context context, IAttributeSet attrs)
			: base (context, attrs)
		{
			_mContext = context;
		}

		public CustomEditText(Context context, IAttributeSet attrs, int defStyle) 
			: base (context, attrs, defStyle)
		{
			_mContext = context;
		}

		public override void SetCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom)
		{
			if (left != null)
				drawableLeft = left;
			if (right != null)
				drawableRight = right;
			if (top != null)
				drawableTop = top;
			if (bottom != null)
				drawableBottom = bottom;
			base.SetCompoundDrawables (left, top, right, bottom);
		}

		protected override void OnSizeChanged(int w, int h, int oldw, int oldh) 
		{
			base.OnSizeChanged(w, h, oldw, oldh);
		}


		public override bool OnTouchEvent (MotionEvent ev)
		{
			Rect bounds;
			if (ev.Action == MotionEventActions.Down) {
				actionX = (int) ev.GetX();
				actionY = (int) ev.GetY();
				if (drawableBottom != null
					&& drawableBottom.Bounds.Contains(actionX, actionY)) 
				{
					if (_mContext is LoginActivity) {
						LoginActivity lActivity = _mContext as LoginActivity;
						lActivity.onClick (NovumRadio.Android.Utils.DrawablePosition.BOTTOM, this);
					}
					return base.OnTouchEvent(ev);
				}

				if (drawableTop != null
					&& drawableTop.Bounds.Contains(actionX, actionY)) 
				{
					if (_mContext is LoginActivity) {
						LoginActivity lActivity = _mContext as LoginActivity;
						lActivity.onClick (NovumRadio.Android.Utils.DrawablePosition.TOP, this);
					}
					return base.OnTouchEvent(ev);
				}

				// this works for left since container shares 0,0 origin with bounds
				if (drawableLeft != null) {
					bounds = null;
					bounds = drawableLeft.Bounds;

					int x, y;
					int extraTapArea = (int) (13 * Resources.DisplayMetrics.Density + 0.5);

					x = actionX;
					y = actionY;

					if (!bounds.Contains(actionX, actionY)) {
						/** Gives the +20 area for tapping. */
						x = (int) (actionX - extraTapArea);
						y = (int) (actionY - extraTapArea);

						if (x <= 0)
							x = actionX;
						if (y <= 0)
							y = actionY;

						/** Creates square from the smallest value */
						if (x < y) {
							y = x;
						}
					}

					if (bounds.Contains(x, y) && _mContext != null) {
						if (_mContext is LoginActivity) {
							LoginActivity lActivity = _mContext as LoginActivity;
							lActivity.onClick (NovumRadio.Android.Utils.DrawablePosition.LEFT, this);
						}
						ev.Action = MotionEventActions.Cancel;
						return false;
					}
				}

				if (drawableRight != null) {

					bounds = null;
					bounds = drawableRight.Bounds;

					int x, y;
					int extraTapArea = 13;

					/**
                 * IF USER CLICKS JUST OUT SIDE THE RECTANGLE OF THE DRAWABLE
                 * THAN ADD X AND SUBTRACT THE Y WITH SOME VALUE SO THAT AFTER
                 * CALCULATING X AND Y CO-ORDINATE LIES INTO THE DRAWBABLE
                 * BOUND. - this process help to increase the tappable area of
                 * the rectangle.
                 */
					x = (int) (actionX + extraTapArea);
					y = (int) (actionY - extraTapArea);

					/**Since this is right drawable subtract the value of x from the width 
                * of view. so that width - tappedarea will result in x co-ordinate in drawable bound. 
                */
					x = Width - x;

					/*x can be negative if user taps at x co-ordinate just near the width.
                 * e.g views width = 300 and user taps 290. Then as per previous calculation
                 * 290 + 13 = 303. So subtract X from getWidth() will result in negative value.
                 * So to avoid this add the value previous added when x goes negative.
                 */

					if(x <= 0){
						x += extraTapArea;
					}

					/* If result after calculating for extra tappable area is negative.
                 * assign the original value so that after subtracting
                 * extratapping area value doesn't go into negative value.
                 */               

					if (y <= 0)
						y = actionY;                

					/**If drawble bounds contains the x and y points then move ahead.*/
					if (bounds.Contains(x, y) && _mContext != null) {
						if (_mContext is LoginActivity) {
							LoginActivity lActivity = _mContext as LoginActivity;
							lActivity.onClick (NovumRadio.Android.Utils.DrawablePosition.RIGHT, this);
						}
						ev.Action = MotionEventActions.Cancel;
						return false;
					}
					return base.OnTouchEvent(ev);
				}           

			}
			return base.OnTouchEvent (ev);
		}


		protected override void JavaFinalize ()
		{
			drawableRight = null;
			drawableBottom = null;
			drawableLeft = null;
			drawableTop = null;
			base.JavaFinalize ();
		}
	}
}
Comment 1 Ram Chandra 2015-02-09 11:03:13 UTC
Created attachment 9713 [details]
Sample Project

I have checked this issue and I am also getting the same behavior.

Steps I followed:

1. Download the attached project.
2. Build and deploy the attached project.

Screencast: http://www.screencast.com/t/1gicn6sAhl9

Application Output: https://gist.github.com/saurabh360/ea38e2c1ca4b8dc67613
IDE logs: https://gist.github.com/saurabh360/7061b44e973ee831988c
Device Logs: https://gist.github.com/saurabh360/b923902add8c8cd5f55b

Environment Info:

=== Xamarin Studio ===

Version 5.7.1 (build 17)
Installation UUID: 6ea47b0d-1852-4aaf-808d-373ff0a5002b
Runtime:
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.iOS ===

Version: 8.6.1.20 (Business Edition)
Hash: 3b3ef43
Branch: 
Build date: 2015-01-24 09:42:21-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/jatin66/Desktop/Backup/android-sdk-macosx
	Supported Android versions:
		1.6    (API level 4)
		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.12.0.4 (Business Edition)

=== Build Information ===

Release ID: 507010017
Git revision: 0bc7d3550b6b088ac25b08dcf7bbe73bcc8658b3
Build date: 2015-02-03 19:43:29-05
Xamarin addins: f7b7d34419c9ec24501bfa7c658e80a6305613e0

=== Operating System ===

Mac OS X 10.10.0
Darwin Jatin66s-iMac.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 Ram Chandra 2015-02-09 11:12:58 UTC
Getting same behavior on windows environment.

Microsoft Visual Studio Professional 2013
Version 12.0.30723.00 Update 3
Microsoft .NET Framework
Version 4.5.51641

Installed Version: Professional

LightSwitch for Visual Studio 2013   06177-004-0447006-02641
Microsoft LightSwitch for Visual Studio 2013

Team Explorer for Visual Studio 2013   06177-004-0447006-02641
Microsoft Team Explorer for Visual Studio 2013

Visual Basic 2013   06177-004-0447006-02641
Microsoft Visual Basic 2013

Visual C# 2013   06177-004-0447006-02641
Microsoft Visual C# 2013

Visual C++ 2013   06177-004-0447006-02641
Microsoft Visual C++ 2013

Visual F# 2013   06177-004-0447006-02641
Microsoft Visual F# 2013

Visual Studio 2013 Code Analysis Spell Checker   06177-004-0447006-02641
Microsoft® Visual Studio® 2013 Code Analysis Spell Checker

Portions of International CorrectSpell™ spelling correction system © 1993 by Lernout & Hauspie Speech Products N.V. All rights reserved.

The American Heritage® Dictionary of the English Language, Third Edition Copyright © 1992 Houghton Mifflin Company. Electronic version licensed from Lernout & Hauspie Speech Products N.V. All rights reserved.

Windows Phone SDK 8.0 - ENU   06177-004-0447006-02641
Windows Phone SDK 8.0 - ENU

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools   12.3.50717.0
Microsoft Web Developer Tools contains the following components:
Support for creating and opening ASP.NET web projects
Browser Link: A communication channel between Visual Studio and browsers
Editor extensions for HTML, CSS, and JavaScript
Page Inspector: Inspection tool for ASP.NET web projects
Scaffolding: A framework for building and running code generators
Server Explorer extensions for Microsoft Azure Websites
Web publishing: Extensions for publishing ASP.NET web projects to hosting providers, on-premises servers, or Microsoft Azure

ASP.NET Web Frameworks and Tools 2012.2   4.1.21001.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.20703.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.2
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Microsoft Advertising SDK for Windows Phone   
Microsoft Advertising SDK for Windows Phone
Build 

Microsoft Azure Mobile Services Tools   1.2
Microsoft Azure Mobile Services Tools

NuGet Package Manager   2.8.50926.663
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Office Developer Tools for Visual Studio 2013 ENU   12.0.30626
Microsoft Office Developer Tools for Visual Studio 2013 ENU

PowerShell Tools   1.2
Provides file classification services using PowerShell

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   12.0.30919.1
Microsoft SQL Server Data Tools

Windows Phone 8.1 SDK Integration   1.0
This package integrates the tools for the Windows Phone 8.1 SDK into the menus and controls of Visual Studio.

Workflow Manager Tools 1.0   1.0
This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin   3.9.293.0 (080a254)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   4.20.0.34 (49a04b966feb40dfdba49d57ba16249b66d606a6)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS   8.6.1.0 (3b3ef438017c7ecf486defa9e01567a5f2b3cb2a)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified
Comment 3 Jonathan Pryor 2015-02-09 11:35:05 UTC
What is your minimum supported android version, Android 2.3 or 3.0+? On which Android version are you seeing this?

The problem is that Java code invoking virtual methods from a constructor is a known "leaky abstraction"; see:

http://developer.xamarin.com/guides/android/under_the_hood/architecture/#Java_Activation

> There are two scenarios in which the (IntPtr, JniHandleOwnership) constructor
> must be manually provided on a Managed Callable Wrapper subclass:
> ...
> 2. Virtual method invocation from a base class constructor.

What's happening here, specifically, is in Attachment #9705 [details]:

> [MonoDroid] System.NotSupportedException: Unable to activate instance of type NovumRadio.CustomEditText from native handle aa30001d ---> System.MissingMethodException: No constructor found for NovumRadio.CustomEditText::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
> [MonoDroid] Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown.
> [MonoDroid]   --- End of managed exception stack trace ---
> [MonoDroid] java.lang.Error: Java callstack:
> [MonoDroid] 	at novumradio.CustomEditText.n_setCompoundDrawables(Native Method)
> [MonoDroid] 	at novumradio.CustomEditText.setCompoundDrawables(CustomEditText.java:47)
> [MonoDroid] 	at android.widget.TextView.setCompoundDrawablesWithIntrinsicBounds(TextView.java:2002)
> [MonoDroid] 	at android.widget.TextView.<init>(TextView.java:1148)
> [MonoDroid] 	at android.widget.EditText.<init>(EditText.java:60)
> [MonoDroid] 	at android.widget.EditText.<init>(EditText.java:56)
> [MonoDroid] 	at novumradio.CustomEditText.<init>(CustomEditText.java:31)
> ...
> [MonoDroid] 	at android.app.Activity.setContentView(Activity.java:1881)
> [MonoDroid] 	at novumradio.android.LoginActivity.n_onCreate(Native Method)

Working backwards, Java calls LoginActivity.n_onCreate(), which calls your  C# LoginActivity.OnCreate() method, which calls Activity.SetContentView(), which loads layout XML, which creates an instance of your CustomEditText type.

The "problem" is that Java doesn't allow `native` constructors, requiring that we generate Java code for constructors, and Java in turn requires that all base constructors -- such as ExtView -- be executed before executing the most derived constructor -- CustomEditText, in this case.

Because the CustomEditText constructor hasn't run yet, there is no corresponding C# instance on which to invoke methods upon.

The `monodroid.apidemo.LogTextBox.getDefaultMovementMethod()` example in the above documentation is ~identical to what you're seeing, just replace getDefaultMovementMethod() with setCompoundDrawables().

The "fix" (workaround) is to add a (IntPtr, JniHandleOwnership) constructor to your type:

    partial class CustomEditText {
        public CustomEditText (IntPtr handle, JniHandleOwnership transfer)
            : base (handle, transfer)
        {
        }
    }

Adding this constructor will fix the NotSupportedException ---> MissingMethodException: the previously missing constructor will now be found, allowing a CustomEditText "wrapper" to be constructed around the Java-side CustomEditText instance. The CustomEditText. SetCompoundDrawables() overriding method will then be invoked, and then "later" the appropriate CustomEditText(Context, ...) constructor will be executed on the same instance.

Timeline of events:

1. Java CustomEditText created
2. Java TextEdit constructor invoked, invokes  setCompoundDrawables()
3. C# EditText.n_setCompoundDrawables() invoked
4. Xamarin.Android creates a CustomEditText "wrapper" around instance of (1)
    by using the (IntPtr, JniHandleOwnership) constructor.
5. Xamarin.Android invokes the overriding
    CustomEditText.SetCompoundDrawables() method.
6. TextEdit constructor finishes.
7. Java CustomEditText constructor executes;
    causes C# CustomEditText  constructor to execute.
7.b: Constructor executes on *same* instance created in (4).

Thus, two constructors will be executed on the same instance.
Comment 4 Deval 2015-02-10 00:07:48 UTC
My project android min version is 4.0.3. And my Xamarin.Android
Version is 4.20.0.

Thanks for the help. Now my app working as charm.
Comment 5 Ram Chandra 2015-09-11 08:30:45 UTC
As per comment 4, this issue doesn't exist. Hence I am closing this issue.