繁体   English   中英

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

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

我使用自定义渲染对文本进行排序,但它在某些设备上不起作用并引发此错误:

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

谢谢,如果有人帮忙。

这是我的代码:



[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;
            }
        }


    }


}

该错误是因为此方法首先在 API 版本 26 的 Android 上引入,因此这意味着在具有旧版本 Android 的设备上它将崩溃。

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

您仍然可以使用它,但您需要在调用它之前验证操作系统版本。 如果你已经在使用 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;
    }
}

注意:验证只会防止崩溃,但这意味着您将不支持操作系统 <8.0 (26) 的设备。

希望这可以帮助。-

我尝试过使用以下代码,它可以在我的本地站点中使用:

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;
            }
        }
    }
}

Android 9.0设备的影响:

在此处输入图像描述

Android 7.0设备的影响:

在此处输入图像描述

暂无
暂无

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

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