jamesmontemagno / SettingsPlugin

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

Android: java.lang.Boolean cannot be cast to java.lang.String #103

Closed ruisilva450 closed 7 years ago

ruisilva450 commented 7 years ago

Bug Information

Version Number of Plugin: 3.1.1 Device Tested On: Xiaomi Redmi Note 4 running Android 7.0 Simulator Tested On: N/A Version of VS: 2017 15.4.2 Version of Xamarin: 2.4.0.38779 Versions of other things you are using: N/A

Steps to reproduce the Behavior

// In Helpers/Settings.cs
public static string Pin
        {
            get => AppSettings.GetValueOrDefault(nameof(Pin), string.Empty);
            set => AppSettings.AddOrUpdateValue(nameof(Pin), value);
        }

2.

// Somewhere on the app
Helpers.Settings.Pin = "somePinEncryptedHashWithAround450Characters";

3.

Close the app.

4.

Reopen the app.

5.

// In App.xaml.cs
string result = Helpers.Settings.Pin;

Expected Behavior

result should have "somePinEncryptedHashWithAround450Characters"

Actual Behavior

Exception:

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

Full Stack

Code snippet

N/A

Screenshots

N/A

jamesmontemagno commented 7 years ago

In the past you must have saved it as a boolean. you should remove the setting and then re-add it.

ruisilva450 commented 7 years ago

Actually I'm doing that with this code on the App.xaml.cs:

try
{
    string result = Helpers.Settings.Pin; // Don't know why this happens...yet!
}
catch (Exception ex)
{
    Helpers.Settings.Pin = string.Empty;
}

This keeps the app running in order to try to fix the issue. Either way, the line in step 2. should have had fixed this by running later in the app:

Helpers.Settings.Pin = "somePinEncryptedHashWithAround450Characters";

Unfortunately, every time I start the app, it falls down on that catch bringing the exception.

I already cleared the cache of the app, uninstalled and reinstalled, and nothing new. Other settings are working normally, for my annoyance.

jamesmontemagno commented 7 years ago

maybe "pin" is reserved. try something else perhaps.

ruisilva450 commented 7 years ago

I had this working and for some reason it stopped. Now, even if I change the "Pin" for "PinHash", it still gives me the error.

EDIT: After some investigation, it turns out that if I put more than 126 characters in the value, it causes this exception. That's some weird exception and a not very specific one. I would suggest to properly check the size of the content when trying to set a given property/setting based on the limits of the platform. Not sure if this is doable or not. Can you add a word about this?