Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

[Bug] java.lang.Throwable: Handle must be valid. Parameter name: type when calling Xamarin.Essentials.Preferences.Set #1301

Open
IntegerOverlord opened this issue Jun 5, 2020 · 5 comments
Labels
bug Something isn't working

Comments

@IntegerOverlord
Copy link

IntegerOverlord commented Jun 5, 2020

Description

In my Xamarin.Forms project I get this error from users a lot:

Fatal Exception: java.lang.Throwable: Handle must be valid.
Parameter name: type
       at Java.Interop.JniEnvironment+InstanceMethods.GetMethodID (Java.Interop.JniObjectReference type, System.String name, System.String signature)(filename unknown)
       at Android.Runtime.JNIEnv.GetMethodID (System.IntPtr kls, System.String name, System.String signature)(filename unknown)
       at Android.Content.ISharedPreferencesInvoker.Edit ()(filename unknown)
       at Xamarin.Essentials.Preferences.PlatformSet[T] (System.String key, T value, System.String sharedName)(filename unknown)
       at Xamarin.Essentials.Preferences.Set (System.String key, System.Int32 value, System.String sharedName)(filename unknown)
       at Xamarin.Essentials.Preferences.Set (System.String key, System.Int32 value)(filename unknown)

I am unable to reproduce it myself, but there are more then a 100 of these reports. The bug happens on every version of Android

Also, I had the same issue with James Montemagno Settings Plugin:
jamesmontemagno/SettingsPlugin#152

Moving to Xamarin.Essentials didn't resolve it for me
And, I found similar issue with SecureStorage here, which was resolved:
#380

Expected Behavior

Preferences are saved

Actual Behavior

Crash

Basic Information

  • Version with issue: 1.5.3.2
  • Last known good version: -
  • IDE: Visual Studio for Mac 2019 Version 8.6.2 (build 6)
  • Platform Target Frameworks:
    • Android: 9.0
  • Android Support Library Version: 28.0.0.3
  • Nuget Packages:
    image
  • Affected Devices: Different devices with Android 10, 9, 8
@IntegerOverlord IntegerOverlord added the bug Something isn't working label Jun 5, 2020
@jamesmontemagno
Copy link
Collaborator

Do you have an example of usage?

@IntegerOverlord
Copy link
Author

@jamesmontemagno hi! Yes, I use it like this

        public static int Transposition
        {
            get => Preferences.Get(PreferencesConstants.Transposition, 0);
            set => Preferences.Set(PreferencesConstants.Transposition, value);
        }

@IntegerOverlord
Copy link
Author

@jamesmontemagno I am unable to reproduce it myself, but in production this exception occurs quite a lot

@guillermo-gerard
Copy link

I found this issue and even though I'm not using Essentials, I want to share how I stopped this from occurring:
I was using just sharedPreferences as I'm working on a xamarin android app, and I was trying to write a value from inside a catch block.
I just took off that from the catch block and I stopped receiving this exception.
I hope this may be useful for someone else

@IntegerOverlord
Copy link
Author

@guillermo-gerard I tried it in a test project, catch block doesn't cause any issues there

        void Button_Clicked(System.Object sender, System.EventArgs e)
        {
            try
            {
                throw new Exception();
            }
            catch
            {
                Preferences.Set("Test", "Test Value");
            }
        }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants