C# Java.Lang.NoSuchMethodError:';“无非静态方法”;Landroid/widget/TextView;。setJustificationMode(I)V“';
我使用自定义渲染对文本进行排序,但它在某些设备上不起作用,并引发以下错误: Java.Lang.NoSuchMethodError:“无非静态方法”Landroid/widget/TextView;.SetRightationMode(I)V“ 如果有人帮忙,谢谢你 这是我的代码:C# Java.Lang.NoSuchMethodError:';“无非静态方法”;Landroid/widget/TextView;。setJustificationMode(I)V“';,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我使用自定义渲染对文本进行排序,但它在某些设备上不起作用,并引发以下错误: Java.Lang.NoSuchMethodError:“无非静态方法”Landroid/widget/TextView;.SetRightationMode(I)V“ 如果有人帮忙,谢谢你 这是我的代码: [assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRender))] namespace customlabel.Droid
[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;
}
}
}
}
[程序集:ExportRenderer(typeof(CustomLabel)、typeof(CustomLabelRender))]
命名空间customlabel.Droid
{
公共类CustomLabelRender:LabelRenderer
{
公共CustomLabelRender(上下文):基础(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
Control.justicationmode=justicationmode.InterWord;
}
}
}
}
错误是因为此方法最初是在API版本26的Android上引入的,这意味着在使用旧版本Android的设备上它将崩溃
更多信息请点击此处:
您仍然可以使用它,但在调用它之前需要验证操作系统版本。如果您已经在使用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;
}
}
注意:验证只会防止崩溃,但这意味着您将不支持带有的设备。我已尝试使用以下代码,并且它在我的本地站点中工作:
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;
}
}
}
}
公共类CustomLabelRenderer:LabelRenderer
{
公共CustomLabelRenderer(上下文):基本(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
如果(Xamarin.Essentials.DeviceInfo.Version.Major>=8)
{
if(控件!=null)
{
SetBackgroundColor(Android.Graphics.Color.AliceBlue);
Control.justicationmode=justicationmode.InterWord;
}
}
其他的
{
if(控件!=null)
{
SetBackgroundColor(Android.Graphics.Color.Red);
Control.TextAlignment=Android.Views.TextAlignment.Center;
}
}
}
}
对安卓9.0设备的影响:
对安卓7.0设备的影响:
这对我不起作用。有没有其他方法可以组织我的文本在所有版本上工作?嗨,你的项目中使用的android目标框架是什么?谢谢你的回答,我的目标框架是11。但当我在安卓7上试用时,我的应用程序崩溃了!好吧,如果是这样的话,派恩达斯的答案应该适合你。你能解释一下为什么这不适合你吗?我不知道,当我使用pinedax的代码时,我的应用程序又崩溃了。谢谢你的提示!@少年蒋-MSFT