Search code examples
c#androidxamarinxamarin.formsnosuchmethoderror

Xamarin Android 6.0 Java.Lang.NoSuchMethodError with View.SetText


I have made a custom renderer for a Label that renders the HTML code as text.

When I try to set the formatted text this erro occurs:

Unhandled Exception: Java.Lang.NoSuchMethodError: no static method "Landroid/text/Html;.fromHtml(Ljava/lang/String;I)Landroid/text/Spanned;" occurred

This is my piece of code:

Control.SetText(Html.FromHtml(View.Text.ToString(), FromHtmlOptions.ModeLegacy), TextView.BufferType.Spannable);

This happens only on Android 6.0, from 7.0 all works fine.

How can I solve this?

There is a workaround?

Thank you!

UPDATE

Thanks to @Jon Douglas i've solved in this way:

 if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.N) {
   Control.SetText(Html.FromHtml(View.Text.ToString(), FromHtmlOptions.ModeLegacy), TextView.BufferType.Spannable);
 } else {
   // For API < 24 
   Control.SetText(Html.FromHtml(View.Text.ToString()), TextView.BufferType.Normal);
 }

For the "else" path, VisualStudio told me that are deprecated but it compiles anyway.


Solution

  • You are using an overloaded method that was introduced in API 24. Thus to properly support this, you will need a runtime check to check for < API 24 values in which you will use a non-API 24 Html.FromHtml overload.

    See the Html class for overloads that you can appropriately use. For example:

    1 Parameter Method for < API 24 - https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String)

    3 Parameter Method for < API 24 - https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String, android.text.Html.ImageGetter, android.text.Html.TagHandler)