Search code examples
androidkotlinandroid-edittextkotlin.notimplementederror

kotlin.NotImplementedError: An operation is not implemented with addTextChangedListenerher on EditText


When I call addTextChangedListener(textWacther) on my EditText and try to run the app, it just closes when something entered in that field. Isn't my way of enabling the button back correct?

class MainActivity : AppCompatActivity() {
lateinit var num1TextE: EditText
lateinit var resultText: TextView
lateinit var plusBtn: Button

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    num1TextE = findViewById(R.id.firstNum_TextE)
    plusBtn = findViewById(R.id.plus_btn)
    resultText = findViewById(R.id.result_TextV)
    resultText.visibility = View.GONE

    plusBtn.isClickable= false
    plusBtn.isEnabled = false
    num1TextE.addTextChangedListener(textWatcher)

    plusBtn.setOnClickListener {
        var num1 = num1TextE.text.toString().toInt()
        var num2 = num2TextE.text.toString().toInt()
        resultText.visibility = View.VISIBLE
        resultText.text = "Result is ${num1 + num2}"
   }
}

var textWatcher = object : TextWatcher {
    override fun afterTextChanged(p0: Editable?) {

    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        var test: String = num1TextE.text.toString()
        if(! test.isEmpty()) {
            plusBtn.isClickable = true
            plusBtn.isEnabled = true
        }
       }
    }
}

Log:

2019-07-31 17:02:01.633 3078-3078/com.example.mycalculator E/InputEventSender: Exception dispatching finished signal.
2019-07-31 17:02:01.633 3078-3078/com.example.mycalculator E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-07-31 17:02:01.637 3078-3078/com.example.mycalculator E/MessageQueue-JNI: kotlin.NotImplementedError: An operation is not implemented: not implemented
    at com.example.mycalculator.MainActivity$textWatcher$1.beforeTextChanged(MainActivity.kt:101)

Solution

  • Remove TODO("not implemented") leaving beforeTextChanged() { } with an empty body if you're not going to use the callback.