Android random crashes when background color is binded

ViikkiViikki USMember ✭✭

I face some random crashes on android when background color is binded. For example if I bind grid and button background colors then I see random crashes when the app is used in fast pace, changing pages etc. and making thing which changes those values. The binding is using a converter, to convert int values into Color.FromHex....

here is some the output window when crash happens:

11-15 16:46:37.206 D/Mono (26956): DllImport attempting to load: '/system/lib/liblog.so'.
11-15 16:46:37.208 D/Mono (26956): DllImport loaded library '/system/lib/liblog.so'.
11-15 16:46:37.208 D/Mono (26956): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
11-15 16:46:37.208 D/Mono (26956): Searching for '__android_log_print'.
11-15 16:46:37.208 D/Mono (26956): Probing '__android_log_print'.
11-15 16:46:37.208 D/Mono (26956): Found as '__android_log_print'.
Thread started: #38
11-15 16:46:37.253 I/MonoDroid(26956): UNHANDLED EXCEPTION:
11-15 16:46:37.343 I/MonoDroid(26956): System.ObjectDisposedException: Cannot access a disposed object.
11-15 16:46:37.343 I/MonoDroid(26956): Object name: 'Xamarin.Forms.Platform.Android.Platform+DefaultRenderer'.
11-15 16:46:37.343 I/MonoDroid(26956): at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00029] in <1d799d87381c4db5b1252825fd105b84>:0
11-15 16:46:37.343 I/MonoDroid(26956): at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00000] in <1d799d87381c4db5b1252825fd105b84>:0
11-15 16:46:37.343 I/MonoDroid(26956): at Android.Views.View.SetBackgroundColor (Android.Graphics.Color color) [0x00028] in :0
11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].UpdateBackgroundColor () [0x00000] in D:\agent\_work\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:381 11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00017] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:266
11-15 16:46:37.343 I/MonoDroid(26956): at (wrapper delegate-invoke) :invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x0000a] in D:\agent_work\1\s\Xamarin.Forms.Core\BindableObject.cs:137
11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in D:\agent_work\1\s\Xamarin.Forms.Core\Element.cs:388
11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x000f4] in D:\agent_work\1\s\Xamarin.Forms.Core\BindableObject.cs:593
11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0015b] in D:\agent_work\1\s\Xamarin.Forms.Core\BindableObject.cs:390
11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x001f9] in D:\agent_work\1\s\Xamarin.Forms.Core\BindingExpression.cs:174
11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in D:\agent_work\1\s\Xamarin.Forms.Core\BindingExpression.cs:56
11-15 16:46:37.343 I/MonoDroid(26956): at Xamarin.Forms.BindingExpression+BindingExpressionPart.b__47_0 () [0x00000] in D:\agent_work\1\s\Xamarin.Forms.Core\BindingExpression.cs:551
11-15 16:46:37.343 I/MonoDroid(26956): at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in :0
11-15 16:46:37.343 I/MonoDroid(26956): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in :0
11-15 16:46:37.343 I/MonoDroid(26956): at (wrapper dynamic-method) System.Object:b4b55aa0-d882-46c8-bacf-2459ba9bfd8b (intptr,intptr)
11-15 16:46:37.381 W/zygote (26956): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
11-15 16:46:37.388 D/Mono (26956): DllImport searching in: '__Internal' ('(null)').
11-15 16:46:37.388 D/Mono (26956): Searching for 'java_interop_jnienv_throw'.
11-15 16:46:37.388 D/Mono (26956): Probing 'java_interop_jnienv_throw'.
11-15 16:46:37.388 D/Mono (26956): Found as 'java_interop_jnienv_throw'.
An unhandled exception occured.

Best Answer

Answers

  • voidstreamvoidstream FRMember ✭✭✭

    Hello,

    Can you drop us the code of the binded color property and the converter?

    Regards

  • ViikkiViikki USMember ✭✭
    edited November 2017

    XAML
    .....

                                                <Grid x:Name="TrafficSignalGrid" WidthRequest="15" HeightRequest="15" HorizontalOptions="Center"
                                                    VerticalOptions="Center" IsVisible="{Binding ViewModel.StatusValid}"
                                                    BackgroundColor="{Binding ViewModel.Status, Converter={StaticResource TrafficLightColor}}">
                                                    <Image x:Name="TrafficLight" Aspect="AspectFit" Source="images/EmptySignal.png" Margin="-1,-1,-1,-1"
                                                           HorizontalOptions="Center" VerticalOptions="Center"/>
                                                </Grid>
    

    ....

    ViewModel
    ....

                    public int Status
                    {
                        get
                        {
                            return MyCheckListItem?.status ?? 0;
                        }
            
                        set
                        {
                            if (MyCheckListItem != null && MyCheckListItem.status != value)
                            {
                                MyCheckListItem.status = value;
                                OnPropertyChanged();
                                OnPropertyChanged("Filled");
                                OnPropertyChanged("ContentNeeded");
                                OnPropertyChanged("StatusValid");
                            }
                        }
                    }
    

    .....

    Converter
    ......

            public class CheckListItemStatusToTrafficLightColorConverter : IValueConverter
            {
                public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                {
                    if ((int)value == 0 )
                        return Color.FromHex(....);
                    if((int)value == 1)
                        return Color.FromHex(....);
                    return Color.FromHex(....);
                }
        
                public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                {
                    throw new NotImplementedException();
                }
            }
    

    .....

  • voidstreamvoidstream FRMember ✭✭✭
    edited November 2017

    @Viikki

    Can you check your converter? Do you know which code create your error?

    For example try this:

                    try
                    {
                        if (value != null)
                        {
                            switch ((int)value)
                            {
                                case 0: return Color.FromHex("#FF0000");
                                case 1: return Color.FromHex("#00FF00");
                                default: return Color.FromHex("#000000");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.InnerException.Message);
                    }
    
  • ViikkiViikki USMember ✭✭

    @voidstream

    Thank you for your help. No, I have no idea which code create the error. however if I have write something to console in converter that is usually the last in console output. If I put try catch in converter as you suggested it never catches any exception at that point. Also the null checking has no effect. Timing is also important here, so crashing app while debug is difficult comparing to just running on hardware.

  • ViikkiViikki USMember ✭✭

    For example I have this kind of output at the time of crash:

    11-17 10:33:46.347 I/mono-stdout(26752): ContentNeededToContentButtonColorConverter
    11-17 10:33:47.942 V/MediaPlayer(26752): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
    11-17 10:33:47.942 V/MediaPlayer(26752): cleanDrmObj: mDrmObj=null mDrmSessionId=null
    11-17 10:33:47.943 V/MediaPlayer(26752): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
    11-17 10:33:47.944 V/MediaPlayer(26752): cleanDrmObj: mDrmObj=null mDrmSessionId=null
    11-17 10:33:48.925 W/ExifInterface(26752): Skip the tag entry since tag number is not defined: 2
    11-17 10:33:49.018 W/View    (26752): requestLayout() improperly called by md5b60ffeb829f638581ab2bb9b1a7f4f3f.ScrollViewContainer{73af37b V.E...... ......ID 0,0-1300,1797} during layout: running second layout pass
    11-17 10:33:49.098 W/View    (26752): requestLayout() improperly called by md5270abb39e60627f0f200893b490a1ade.NavigationPageRenderer{5045a00 V.E...... ........ 0,0-1440,2392 #1} during second layout pass: posting in next frame
    11-17 10:33:49.366 D/Mono    (26752): GC_BRIDGE waiting for bridge processing to finish
    11-17 10:33:49.371 I/zygote  (26752): Explicit concurrent copying GC freed 8696(543KB) AllocSpace objects, 6(140KB) LOS objects, 50% free, 2MB/4MB, paused 296us total 30.479ms
    11-17 10:33:49.373 D/Mono    (26752): GC_TAR_BRIDGE bridges 380 objects 817 opaque 180 colors 377 colors-bridged 376 colors-visible 376 xref 4 cache-hit 0 cache-semihit 0 cache-miss 1 setup 0.12ms tarjan 0.50ms scc-setup 0.16ms gather-xref 0.01ms xref-setup 0.00ms cleanup 0.16ms
    11-17 10:33:49.373 D/Mono    (26752): GC_BRIDGE: Complete, was running for 34.85ms
    11-17 10:33:49.373 D/Mono    (26752): GC_MAJOR: (user request) time 23.92ms, stw 25.49ms los size: 1024K in use: 161K
    11-17 10:33:49.374 D/Mono    (26752): GC_MAJOR_SWEEP: major size: 5632K in use: 3967K
    ContentNeededToContentButtonColorConverter
    11-17 10:33:49.447 I/mono-stdout(26752): ContentNeededToContentButtonColorConverter
    11-17 10:33:54.714 W/View    (26752): requestLayout() improperly called by md5b60ffeb829f638581ab2bb9b1a7f4f3f.ScrollViewContainer{d0d7468 V.E...... ......ID 0,0-1300,1724} during layout: running second layout pass
    11-17 10:33:54.781 W/View    (26752): requestLayout() improperly called by md5270abb39e60627f0f200893b490a1ade.NavigationPageRenderer{5045a00 V.E...... ......ID 0,0-1440,2308 #1} during second layout pass: posting in next frame
    11-17 10:34:01.719 I/mono-stdout(26752): ContentNeededToContentButtonColorConverter
    ContentNeededToContentButtonColorConverter
    Thread finished: <Thread Pool> #25
    Thread started: <Thread Pool> #26
    The thread 'Unknown' (0x19) has exited with code 0 (0x0).
    11-17 10:34:04.078 I/zygote  (26752): Explicit concurrent copying GC freed 15265(1013KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 2MB/4MB, paused 177us total 23.872ms
    11-17 10:34:04.082 D/Mono    (26752): GC_TAR_BRIDGE bridges 371 objects 43209 opaque 23893 colors 348 colors-bridged 335 colors-visible 335 xref 29 cache-hit 0 cache-semihit 0 cache-miss 13 setup 0.08ms tarjan 22.77ms scc-setup 0.08ms gather-xref 0.02ms xref-setup 0.02ms cleanup 1.23ms
    11-17 10:34:04.082 D/Mono    (26752): GC_BRIDGE: Complete, was running for 28.64ms
    11-17 10:34:04.082 D/Mono    (26752): GC_MINOR: (Nursery full) time 41.88ms, stw 43.04ms promoted 1272K major size: 6496K in use: 5312K los size: 1024K in use: 161K
    ContentNeededToContentButtonColorConverter
    11-17 10:34:08.349 I/mono-stdout(26752): ContentNeededToContentButtonColorConverter
    An unhandled exception occured.
    
  • voidstreamvoidstream FRMember ✭✭✭

    @Viikki said:
    @voidstream

    Thank you for your help. No, I have no idea which code create the error. however if I have write something to console in converter that is usually the last in console output. If I put try catch in converter as you suggested it never catches any exception at that point. Also the null checking has no effect. Timing is also important here, so crashing app while debug is difficult comparing to just running on hardware.

    Can you comment/delete backgroundColor property in the XAML, are you sure it's the backgroundColor property the source problem?

  • ViikkiViikki USMember ✭✭

    @voidstream
    Yes, if I don't use binding in those background colors, then I don't get these crashes.

  • ViikkiViikki USMember ✭✭

    @voidstream
    I am not able to reproduce those crashes if I do binding like you suggested. Although, intellisense doesn't recognize those.

  • voidstreamvoidstream FRMember ✭✭✭

    @Viikki said:
    @voidstream
    I am not able to reproduce those crashes if I do binding like you suggested. Although, intellisense doesn't recognize those.

    So it's works? Which answer you used?

  • ViikkiViikki USMember ✭✭

    @voidstream
    I used the following solution:
    BackgroundColor="{Binding ViewModel.Status, Converter={converters:CheckListItemStatusToTrafficLightColorConverter}}">

    Strange, I always used converters with static resource, first defining a key to them at the top of my XAML. That I learned in Windows phone days and never looked back :-)

    But thank you for your help. We don't know exact cause but problem solved.

Sign In or Register to comment.