0

I have following code:

private fun initSlider() {
    binding.sSlider.addOnChangeListener { _, value: Float, _ ->
        if (!value.isNaN()) {
            viewModel.setValueFromSlider(value.roundToInt())
        }
    }
    viewModel.sliderProgress.observe(viewLifecycleOwner) {
       binding.sSlider.value = it?.toFloat() ?: 0f
    }
}

It's just a simple slider, that is used to set some value to the application. The slider looks like this:

<com.google.android.material.slider.Slider
    android:id="@+id/s_slider"
    ...
    android:valueFrom="0"
    android:valueTo="100"
    />

It works well, it is tested on thousands of installations. However, for a some time, there was this strange issue on Transsion Tecno devices:

Fatal Exception: java.lang.IllegalStateException: Slider value(107.0) must be greater or equal to valueFrom(0.0), and lower or equal to valueTo(100.0) at com.google.android.material.slider.BaseSlider.validateValues(BaseSlider.java:621)...

I put these valueFrom and valueTo attributes to Slider as you can see, but the issue still appears. It seems that it is hard crash, the app stops working for these devices. Can somebody advise, how to solve this issue? I do not own such phone. Thank you.

1
  • 1
    Could you provide full error stack? Commented Jan 18 at 7:54

1 Answer 1

0

By looking at the source code of BasicSlider and Slider, I can tell setValues can be called from 2 places: setValue and onRestoreInstanceState. As one of ideas, you can extend Slider class and override these 2 methods and add validation like:

@Override 
public void setValue(float value) {
  setValues(Math.min(value, 100f));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.