.NET MAUI Android Release模式自定义视图导航崩溃问题求助
.NET MAUI Android Release模式下自定义视图导航崩溃问题
我的.NET MAUI Android应用仅在Release模式下崩溃,几乎每次导航到包含自定义视图MyLinkView的页面时都会触发。
MyLinkView代码
XAML代码
<ContentView.Content> <Label x:Name="LabelControl" Style="{Binding TextStyle, Source={x:Reference MyLabelView}}"/> </ContentView.Content>
后台代码
public static readonly BindableProperty TextProperty = BindableProperty.Create(propertyName: nameof(Text), returnType: typeof(string), declaringType: typeof(MyLinkView), defaultValue: string.Empty, propertyChanged: OnTextPropertyChanged); public string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); } public static readonly BindableProperty PressCommandProperty = BindableProperty.Create( propertyName: nameof(PressCommand), returnType: typeof(IAsyncCommand<MyObj>), declaringType: typeof(MyLinkView), defaultValue: null); public IAsyncCommand<MyObj> PressCommand { get => (IAsyncCommand<MyObj>)GetValue(PressCommandProperty); set => SetValue(PressCommandProperty, value); } private async static void OnTextPropertyChanged(BindableObject bindable, object oldValue, object newValue) { try { if (!(bindable is MyLinkView view)) return; if (newValue is null || newValue is string str && string.IsNullOrWhiteSpace(str)) { view.LabelControl.FormattedText = string.Empty; } else if (newValue is string text) { var formattedText = new FormattedString(); foreach (var item in MyLinkHelper.ProcessString(text)) { var span = new Span { Text = item.Text }; var hasLink = !string.IsNullOrEmpty(item.Link); span.GestureRecognizers.Add(new TapGestureRecognizer { Command = view.PressCommand, CommandParameter = view.PressCommandParameter }); formattedText.Spans.Add(span); } view.LabelControl.FormattedText = formattedText; } } catch (Exception ex) { await App.Current.MainPage.DisplayAlert("Alert", ex.Message, "OK"); } }
错误信息
AppCenter上报的错误:
androidx.core.view.ViewCompat$Api29Impl.getAccessibilityDelegate ViewCompat.java, line 5436 Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.view.View$AccessibilityDelegate android.view.View.getAccessibilityDelegate()' on a null object reference
完整堆栈追踪:
Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* ) Java.Interop.JniPeerMembers.JniStaticMethods.InvokeObjectMethod(String , JniArgumentValue* ) AndroidX.Core.View.ViewCompat.GetAccessibilityDelegate(View ) Microsoft.Maui.Controls.Platform.SemanticExtensions.AddOrRemoveControlsAccessibilityDelegate(View virtualView) Microsoft.Maui.Controls.Platform.GesturePlatformManager.GestureCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.IGestureRecognizer, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnCollectionChanged(NotifyCollectionChangedEventArgs ) System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.IGestureRecognizer, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnCollectionChanged(NotifyCollectionChangedAction , Object , Int32 ) System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.IGestureRecognizer, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].InsertItem(Int32 , IGestureRecognizer ) System.Collections.ObjectModel.Collection`1[[Microsoft.Maui.Controls.IGestureRecognizer, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Add(IGestureRecognizer item) Microsoft.Maui.Controls.Label.SetupSpanGestureRecognizers(IEnumerable gestureRecognizers) Microsoft.Maui.Controls.Label.SetupSpans(IEnumerable spans) Microsoft.Maui.Controls.Label.<>c.<.cctor>b__124_0(BindableObject bindable, Object oldvalue, Object newvalue) Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity) Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) Microsoft.Maui.Controls.Label.set_FormattedText(FormattedString value) MyApp.MyLabelView.OnTextPropertyChanged(BindableObject bindable, Object oldValue, Object newValue) Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
测试更新
- 更新1:注释掉给Span添加
TapGestureRecognizer的代码后,崩溃不再发生。 - 更新2:进一步测试发现,当Span包含手势并赋值给
Label.FormattedText时,会在view.LabelControl.FormattedText = formattedText;处崩溃,无手势则正常显示。
有人遇到过类似问题吗?
内容的提问来源于stack exchange,提问作者Rajzer
相关产品推荐
相关产品推荐

