Anonymous
.NET MAUI — Android — NullPointerException — androidx.core.view.ViewCompat$Api29Impl.getAccessibilityDelegate
Сообщение
Anonymous » 16 янв 2025, 13:25
Приложение аварийно завершает работу только в
выпуске и почти каждый раз, когда я перехожу на страницу, где присутствует пользовательское представление.
Вот код MyLinkView:< /p>
код программной части:
Код: Выделить всё
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), declaringType: typeof(MyLinkView), defaultValue: null);
public IAsyncCommand PressCommand
{
get => (IAsyncCommand)GetValue(PressCommandProperty);
set => SetValue(PressCommandProperty, value);
}
private async static void OnTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
try
{
if (!(bindable is MyLinkViewview))
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");
}
}
Я использую DisplayAlert, чтобы перехватить исключение в режиме выпуска.
Вот отчеты из 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
And the Stack Trace is:
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.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)
Я уже несколько дней занимаюсь этой проблемой. Когда я добавляю блок try/catch, я фиксирую сбой, и приложение продолжает работать нормально, но я хочу выяснить, в чем проблема. У кого-нибудь раньше была подобная проблема?
**ОБНОВЛЕНИЕ: **
Если я прокомментирую этот код:
Код: Выделить всё
//span.GestureRecognizers.Add(new TapGestureRecognizer
//{
// Command = view.PressCommand,
// CommandParameter = view.PressCommandParameter
//});
Сбой больше не происходит, но до сих пор не знаю, почему и какой элемент в собственном коде Android имеет значение null
Подробнее здесь:
https://stackoverflow.com/questions/793 ... ompatapi29
1737023138
Anonymous
Приложение аварийно завершает работу только в [b]выпуске[/b] и почти каждый раз, когда я перехожу на страницу, где присутствует пользовательское представление. Вот код MyLinkView:< /p> [code] [/code] код программной части: [code] 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), declaringType: typeof(MyLinkView), defaultValue: null); public IAsyncCommand PressCommand { get => (IAsyncCommand)GetValue(PressCommandProperty); set => SetValue(PressCommandProperty, value); } private async static void OnTextPropertyChanged(BindableObject bindable, object oldValue, object newValue) { try { if (!(bindable is MyLinkViewview)) 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"); } } [/code] Я использую DisplayAlert, чтобы перехватить исключение в режиме выпуска. Вот отчеты из AppCenter: [code]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 And the Stack Trace is: 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.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) [/code] Я уже несколько дней занимаюсь этой проблемой. Когда я добавляю блок try/catch, я фиксирую сбой, и приложение продолжает работать нормально, но я хочу выяснить, в чем проблема. У кого-нибудь раньше была подобная проблема? **ОБНОВЛЕНИЕ: ** Если я прокомментирую этот код: [code] //span.GestureRecognizers.Add(new TapGestureRecognizer //{ // Command = view.PressCommand, // CommandParameter = view.PressCommandParameter //}); [/code] Сбой больше не происходит, но до сих пор не знаю, почему и какой элемент в собственном коде Android имеет значение null Подробнее здесь: [url]https://stackoverflow.com/questions/79360956/net-maui-android-nullpointerexception-androidx-core-view-viewcompatapi29[/url]