Xamarin.forms: Null reference exception on some Android devices - Xamarin.Forms.Platform.Android.PageRenderer.Xamarin.Forms.Platform.Android.IOrderedTraversalController.UpdateTraversalOrder

Created on 2 Aug 2019  路  6Comments  路  Source: xamarin/Xamarin.Forms

Description

A null reference exception sometimes occur in Xamarin.Forms.Platform.Android.PageRenderer.Xamarin.Forms.Platform.Android.IOrderedTraversalController.UpdateTraversalOrder.

  • Affected platforms: Android
  • Affected build mode: Debug, Release
  • Affected devices:

    • Xiaomi Redmi 6

    • Xiaomi Mi 8

    • Samsung S8

  • Unaffected devices:

    • Emulators
    • Xiaomi Redmi Note 7
    • LG G6
    • Google Nexus 5
    • Sony Xperia
    • Motorola G6
    • Xiaomi Redmi 4X
    • Nokia 8

Steps to Reproduce

using Xamarin.Forms;

namespace TestCSharpXamarinForms
{
    public partial class App : Application
    {
        private bool _flag = true;

        public App()
        {
            InitializeComponent();

            var button = new Button { Text = "Continue" };
            button.Clicked += (_, __) =>
            {
                var view = _flag ? (View)new Entry { Text = "Press 1 time to crash" } : new Label { Text = "I'm shown, bug fixed!" };
                _flag ^= true;
                ((MainPage as ContentPage).Content as StackLayout).Children[1] = view;
            };
            MainPage = new ContentPage
            {
                Content = new StackLayout
                {
                    Children =
                    {
                        button,
                        new Label { Text = "Press 2 times to crash" }
                    }
                }
            };
        }
    }
}

Expected Behavior

I'm shown, bug fixed! is shown after 2 presses on Continue.

Actual Behavior

image

System.NullReferenceException: Object reference not set to an instance of an object.
  at Xamarin.Forms.Platform.Android.PageRenderer.Xamarin.Forms.Platform.Android.IOrderedTraversalController.UpdateTraversalOrder () [0x000a4] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\PageRenderer.cs:177 
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].UpdateParentPageTraversalOrder () [0x00025] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:404 
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00116] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:369 
  at Xamarin.Forms.Platform.Android.ViewRenderer`2[TView,TNativeView].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\ViewRenderer.cs:188 
  at Xamarin.Forms.Platform.Android.EntryRendererBase`1[TControl].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x002db] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\EntryRenderer.cs:251 
  at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
  at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:211 
  at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:353 
  at Xamarin.Forms.Element.set_Parent (Xamarin.Forms.Element value) [0x00094] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:210 
  at Xamarin.Forms.Element.OnChildRemoved (Xamarin.Forms.Element child) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:335 
  at Xamarin.Forms.VisualElement.OnChildRemoved (Xamarin.Forms.Element child) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:750 
  at Xamarin.Forms.Layout`1[T].OnChildRemoved (Xamarin.Forms.Element child) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:41 
  at Xamarin.Forms.Layout.OnInternalRemoved (Xamarin.Forms.View view) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:464 
  at Xamarin.Forms.Layout.InternalChildrenOnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0002b] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:427 
  at (wrapper delegate-invoke) <Module>.invoke_void_object_NotifyCollectionChangedEventArgs(object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
  at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] in <ca7419b40e504a6dbe088f6fe95d09aa>:0 
  at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object oldItem, System.Object newItem, System.Int32 index) [0x0000b] in <ca7419b40e504a6dbe088f6fe95d09aa>:0 
  at System.Collections.ObjectModel.ObservableCollection`1[T].SetItem (System.Int32 index, T item) [0x0001c] in <ca7419b40e504a6dbe088f6fe95d09aa>:0 
  at System.Collections.ObjectModel.Collection`1[T].set_Item (System.Int32 index, T value) [0x0002b] in <ff07eae8184a40a08e79049bbcb31a0e>:0 
  at Xamarin.Forms.ObservableWrapper`2[TTrack,TRestrict].set_Item (System.Int32 index, TRestrict value) [0x00029] in D:\a\1\s\Xamarin.Forms.Core\ObservableWrapper.cs:140 
  at TestCSharpXamarinForms.App.<.ctor>b__1_0 (System.Object _, System.EventArgs __) [0x0003d] in C:\Users\Admin\source\repos\XamarinFormsNRE\TestCSharpXamarinForms\App.xaml.cs:18 
  at Xamarin.Forms.Button.Xamarin.Forms.Internals.IButtonElement.PropagateUpClicked () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Button.cs:177 
  at Xamarin.Forms.ButtonElement.ElementClicked (Xamarin.Forms.VisualElement visualElement, Xamarin.Forms.Internals.IButtonElement ButtonElementManager) [0x0001f] in D:\a\1\s\Xamarin.Forms.Core\ButtonElement.cs:61 
  at Xamarin.Forms.Button.SendClicked () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Button.cs:163 
  at Xamarin.Forms.Platform.Android.ButtonElementManager.OnClick (Xamarin.Forms.VisualElement element, Xamarin.Forms.IButtonController buttonController, Android.Views.View v) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\ButtonElementManager.cs:25 
  at Xamarin.Forms.Platform.Android.FastRenderers.ButtonRenderer.Android.Views.View.IOnClickListener.OnClick (Android.Views.View v) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\FastRenderers\ButtonRenderer.cs:71 
  at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x00011] in <a2f1be6565a84d5393d8f9efa19f479b>:0 
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.42(intptr,intptr,intptr)

Fix

image

ve.GetRenderer().View -> ve.GetRenderer()?.View fixes this bug.

Problems caused

https://www.telerik.com/forums/null-ref-error-iorderedtraversalcontroller-updatetraversalorder
https://www.telerik.com/forums/clearcachedviews
https://github.com/fsprojects/Fabulous/issues/518
https://github.com/fsprojects/Fabulous/issues/535

Basic Information

  • Version with issue: 4.0.0+
  • Last known good version: 3.6.0
  • IDE: Visual Studio for Windows 16.2.0
  • Platform Target Frameworks:

    • Android: 9.0

  • Android Support Library Version: 28.0.0.1
  • Nuget Packages: Xamarin.Forms
  • Affected Devices: (See Description above)

Screenshots

(See Actual Behavior above)

Reproduction Link

TestCSharpXamarinForms.zip

unverified bug

Most helpful comment

I don't know about Add specifically, but RemoveAt followed by Insert works without null reference exception.

All 6 comments

Fix is at #7050.

Does it happen if you use Children.Add(...) rather than Children[1] == ...?

I don't know about Add specifically, but RemoveAt followed by Insert works without null reference exception.

I麓ll try that in the mean while.
Thanks!

Looks like this one is slated for 4.3... Any chance this could be backported to 4.2 SR?

Thanks.

4.3.0-pre1 should be out any time now.

Was this page helpful?
0 / 5 - 0 ratings