'onCreate' overrides Nothing - Kotlin

Asked

Viewed 67 times

-1

I’m new in the area and had made a currency converter application in Kotlin and wanted to put a API, but I am unable to start the application because of this error in the onCreate. I have tried to create a function with the code and call it in onCreate, but ask for a parameter when I do that.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?, dolarResult: DolarResult) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btCalcular = btnCalculator
        btCalcular.setOnClickListener {
            val calculo = Integer.parseInt(txtWrite.text.toString())

            fun HideKeyboard() {
                val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                if (imm.isActive) imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
            }
            
            HideKeyboard()

            val bid = dolarResult.bid

            val resultado = calculo * bid
            val textofinal = "O valor de R$$calculo em USD é $$resultado"
            textView.text = textofinal
        }
    }
}
  • what error you are receiving?

  • 2

    " 'onCreate' overrides Nothing"

1 answer

0

Welcome to Stack Overflow.

The method onCreate should receive only the parameter savedInstanceState, without the dolarResult. The error indicates that you are trying to overwrite a method that does not exist in the parent class (i.e. there is no method called onCreate who receives a Bundle and a DolarResult in class AppCompatActivity).

To solve your build problem, replace your header onCreate for:

override fun onCreate(savedInstanceState: Bundle?)

If you need this parameter at your Activity startup, there are other ways to do this. But you really can’t pass it (or anything else) by onCreate.

  • In my case I need to make a calculation between a typed value and a result of an api (which brings in real time the dollar price)

  • You can pass the data you need on your Activity startup through the Intent that launched it, and deal with onCreate. Have a look at https://developer.android.com/training/basics/firstapp/starting-activity?hl=pt-br. But about the problem described above, declaring onCreate as shown here should solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.