简体   繁体   English

Java.Lang.NoSuchMethodError: '没有非静态方法“Landroid/widget/TextView;.setJustificationMode(I)V”'

[英]Java.Lang.NoSuchMethodError: 'no non-static method “Landroid/widget/TextView;.setJustificationMode(I)V”'

I used the Custom render to sort the text but it does not work on some devices and throws this error:我使用自定义渲染对文本进行排序,但它在某些设备上不起作用并引发此错误:

Java.Lang.NoSuchMethodError: 'no non-static method "Landroid/widget/TextView;.setJustificationMode(I)V"' Java.Lang.NoSuchMethodError: '没有非静态方法“Landroid/widget/TextView;.setJustificationMode(I)V”'

Thanks if anyone helps.谢谢,如果有人帮忙。

This is my code:这是我的代码:



[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRender))]


namespace customlabel.Droid


{


    public class CustomLabelRender : LabelRenderer


    {
        public CustomLabelRender(Context context) : base(context)
        {

        }


        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.JustificationMode = JustificationMode.InterWord;
            }
        }


    }


}

The error is because this method was first introduced on Android on API Version 26 so that means that on devices with older versions of Android it will crash.该错误是因为此方法首先在 API 版本 26 的 Android 上引入,因此这意味着在具有旧版本 Android 的设备上它将崩溃。

More info here: https://developer.android.com/reference/android/widget/TextView#setJustificationMode(int)更多信息: https://developer.android.com/reference/android/widget/TextView#setJustificationMode(int)

You can still use it but you will need to validate the OS Version before calling it.您仍然可以使用它,但您需要在调用它之前验证操作系统版本。 If you are already using Xamarin.Essentials (I guess you are) you can do something like this:如果你已经在使用 Xamarin.Essentials (我猜你是),你可以这样做:

//Version 8.0 is API 26 (https://source.android.com/setup/start/build-numbers)

if (Xamarin.Essentials.DeviceInfo.Version.Major >= 8)
{
    if (Control != null)
    {
        Control.JustificationMode = JustificationMode.InterWord;
    }
}

Note: The validation will only prevent the crash but that means you will not be supporting devices with <8.0 (26) OS.注意:验证只会防止崩溃,但这意味着您将不支持操作系统 <8.0 (26) 的设备。

Hope this helps.-希望这可以帮助。-

I have tried with following code and it works in my local site:我尝试过使用以下代码,它可以在我的本地站点中使用:

public class CustomLabelRenderer: LabelRenderer
{
    public CustomLabelRenderer(Context context) : base(context)
    {

    }


    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        if (Xamarin.Essentials.DeviceInfo.Version.Major >= 8)
        {
            if (Control != null)
            {
                Control.SetBackgroundColor(Android.Graphics.Color.AliceBlue);
                Control.JustificationMode = JustificationMode.InterWord;
            }
        }
        else
        {
            if (Control != null)
            {
                Control.SetBackgroundColor(Android.Graphics.Color.Red);
                Control.TextAlignment = Android.Views.TextAlignment.Center;
            }
        }
    }
}

The effect on Android 9.0 device:Android 9.0设备的影响:

在此处输入图像描述

The effect on Android 7.0 device:Android 7.0设备的影响:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM