halkar / xamarin-range-slider

Range slider for Xamarin and Xamarin.Forms
https://www.nuget.org/packages/Xamarin.Forms.RangeSlider/
MIT License
90 stars 51 forks source link

InvalidCastException in Xamarin.RangeSlider.RangeSliderControl.OnRestoreInstanceState #166

Open joshsutterfield opened 4 years ago

joshsutterfield commented 4 years ago

Unable to duplicate but have seen InvalidCastException logged in crashes coming from here. Most likely one of the two casts is casting null. Cannot fix or bypass with override since Android requires base.OnRestoreInstanceState to be called.

        protected override void OnRestoreInstanceState(IParcelable parcel)
        {
            Bundle bundle = (Bundle)parcel;
            base.OnRestoreInstanceState((IParcelable)bundle.GetParcelable("SUPER"));
            NormalizedMinValue = bundle.GetFloat("MIN");
            NormalizedMaxValue = bundle.GetFloat("MAX");
        }

Suggest alternate code:

            if (parcel is Bundle bundle)
            {
                base.OnRestoreInstanceState(bundle.GetParcelable("SUPER") as IParcelable);
                NormalizedMinValue = bundle.GetFloat("MIN");
                NormalizedMaxValue = bundle.GetFloat("MAX");
            }
            else
            {
                base.OnRestoreInstanceState(parcel);
            }
halkar commented 4 years ago

Can you provide a sample project to reproduce the problem?