jamesmontemagno / SettingsPlugin

Read and Write Settings Plugin for Xamarin and Windows
MIT License
324 stars 80 forks source link

Odd crash on older Android Device: java.lang.String cannot be cast to java.lang.Integer #109

Closed codebeaulieu closed 6 years ago

codebeaulieu commented 6 years ago

https://gist.github.com/codebeaulieu/6bbdcfd61c56d3ae09f11836fb7426b4

Bug Information

Version Number of Plugin: 3.1.1 Device Tested On: Nexus 5 Simulator Tested On: Version of VS: 7.3 (build 799) Version of Xamarin: 2.5.0.121934 Versions of other things you are using: ReactiveUI 6.5

Steps to reproduce the Behavior

in Forms project add a Settings getter and setter:

    public static int GetDeckSizeForStack(int stackId)
    => AppSettings.GetValueOrDefault($"StackDeckSize_{stackId}", 30);

    public static void SetDeckSizeForStack(int stackId, double value)
    { 
        AppSettings.AddOrUpdateValue($"StackDeckSize_{stackId}", value);
    }

Then, in the forms application, attempt to fetch the value:

var deckSize = Values.Settings.GetDeckSizeForStack(id);

Expected Behavior

In iOS this works as expected. I call into the method and it returns an integer.

Actual Behavior

On fresh install or otherwise on an older Android device I get a crash. When calling GetDeckSizeForStack the application crashes.

Java.Lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

for full stack trace see Gist

jamesmontemagno commented 6 years ago

Yes, your issue here is that you are setting a Double and trying to get back an Int, it doesn't work that way.

So... a few options:

 public static int GetDeckSizeForStack(int stackId)
    => AppSettings.GetValueOrDefault($"StackDeckSize_{stackId}", 30);

//change to int
    public static void SetDeckSizeForStack(int stackId, int value)
    { 
        AppSettings.AddOrUpdateValue($"StackDeckSize_{stackId}", value);
    }

or change to double for getter

static double defaultValue = 30;
        public static double GetDeckSizeForStack(int stackId)
            => AppSettings.GetValueOrDefault($"StackDeckSize_{stackId}", defaultValue);

    public static void SetDeckSizeForStack(int stackId, double value)
    { 
        AppSettings.AddOrUpdateValue($"StackDeckSize_{stackId}", value);
    }
codebeaulieu commented 6 years ago

ugg, That worked... facepalm city. Thanks James