Page 1 of 1

.NET MAUI – Android – NullPointerException – androidx.core.view.ViewCompat$Api29Impl.getAccessibilityDelegate

Posted: 16 Jan 2025, 11:25
by Guest
Die Anwendung stürzt nur in Release und fast jedes Mal ab, wenn ich zu der Seite navigiere, auf der eine benutzerdefinierte Ansicht vorhanden ist.
Hier ist der Code für MyLinkView:< /p>

Code: Select all

 


der Code-Behind:

Code: Select all

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

}
Ich verwende DisplayAlert, um die Ausnahme im Release-Modus abzufangen.
Hier sind Berichte von AppCenter:

Code: Select all

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)

Ich bin seit ein paar Tagen mit diesem Problem beschäftigt. Wenn ich den Try/Catch-Block hinzufüge, kommt es zu einem Absturz und die App funktioniert weiterhin normal, aber ich möchte herausfinden, wo das Problem liegt. Hatte jemand schon einmal ein ähnliches Problem?
**UPDATE: **
Wenn ich diesen Code kommentiere:

Code: Select all

           //span.GestureRecognizers.Add(new TapGestureRecognizer
//{
//    Command = view.PressCommand,
//    CommandParameter = view.PressCommandParameter
//});
Der Absturz tritt nicht mehr auf, aber ich weiß immer noch nicht, warum und welches Element im nativen Android-Code null ist