Marduk
Marduk

Reputation: 129

Exception in TextWatcher Kotlin - java.lang.NumberFormatException: For input string: "interface kotlin.jvm.functions.Function0"

I have implemented a TextWatcher for my EditText

txt_height!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                isExecuteSeekbar = false
            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun afterTextChanged(editable: Editable) {

                var height = txt_height!!.text.toString().trim { it <= ' ' }
                if (height.equals(".", ignoreCase = true))
                    txt_height!!.setText("2.0")

                height = txt_height!!.text.toString().trim { it <= ' ' }
                if (!sh!!.check_blank_data(height) && isExecute) {
                    val h = height.toFloat()
                    if (rdo_feet!!.isChecked) {
                        for (k in height_feet_lst.indices) {
                            if (height.equals(height_feet_lst[k], ignoreCase = true)) {
                                pickerFeet!!.selectedItem = k
                                break
                            }
                        }
                    } else {
                        for (k in height_cm_lst.indices) {
                            if (h == height_cm_lst[k].toFloat()) {
                                pickerCM!!.selectedItem = k
                                break
                            }
                        }
                    }
                    saveData()
                }

                isExecuteSeekbar = true
            }
        })

This line generates the error

val h = height.toFloat()

Debugging the code I noticed that the height variable is equivalent to "interface kotlin.jvm.functions.Function0", the default for the edit text is "175" but this txt_height!!.text.toString().trim { it <= ' ' } return me "interface kotlin.jvm.functions.Function0"

Maybe I did something wrong?

EDIT

Using of txt_height

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initHeight()

        body()
    }

fun initHeight() {
        heightList.clear()
        for (k in 60..240) {
            heightList.add("" + k)
        }
        val st = arrayOfNulls<CharSequence>(heightList.size)
        for (k in heightList.indices) {
            st[k] = "" + heightList[k]
        }
        picker!!.values = st
        picker!!.sideItems = 1
        picker!!.setOnItemSelectedListener { index ->
            txt_height!!.setText(st[index])
            
        }
    }

fun body(){
if (!sh!!.check_blank_data(SharedPreferencesManager.height)) {
            if (rdo_cm!!.isChecked) {
                txt_height!!.filters = arrayOf<InputFilter>(DigitsInputFilter(3, 0, 240.0))
                txt_height!!.setText(SharedPreferencesManager.height.toString())
            } else {
                txt_height!!.filters =
                    arrayOf<InputFilter>(InputFilterRange(0.00, height_feet_elements))
                txt_height!!.setText(SharedPreferencesManager.height.toString())
            }
        } else {
            if (rdo_cm!!.isChecked) {
                txt_height!!.filters = arrayOf<InputFilter>(DigitsInputFilter(3, 0, 240.0))
                txt_height!!.setText("150")
            } else {
                txt_height!!.filters =
                    arrayOf<InputFilter>(InputFilterRange(0.00, height_feet_elements))
                txt_height!!.setText("5.0")
            }
        }
}

object SharedPreferencesManager {
    private val ctx: Context
        get() = Application.instance

    private fun createSharedPreferences(): SharedPreferences {
        return ctx.getSharedPreferences(AppUtils.USERS_SHARED_PREF, Context.MODE_PRIVATE)
    }

    private val sharedPreferences by lazy { createSharedPreferences() }

    var height: String
    get() = sharedPreferences.getString(AppUtils.HEIGHT,"")!!
    set(value) = sharedPreferences.edit().putString(AppUtils.HEIGHT, value).apply()


}

Upvotes: 0

Views: 46

Answers (1)

Jaydip Ramani
Jaydip Ramani

Reputation: 88

I think you want to first parse in integer then in float.

val heightInt: Int = Integer.parseInt(height)
val h = heightInt.toFloat()

Upvotes: 1

Related Questions