-1

this is my code for Main activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

fun onButton(view: View?){
    if(view != null) {
        if (input1.text != null && input2.text != null) {
            output.text = (input1.toString().toInt() + input2.toString().toInt()).toString()
        }
    }
}
}

I am getting an error at the toInt() part stating it being an unresolved symbol, any help appreciated

Anjani Mittal
  • 507
  • 1
  • 7
  • 19
  • 1
    try this `input1.text.toString().toInt() + input2.text.toString().toInt()` – AskNilesh Sep 11 '18 at 11:51
  • 1
    try `input1.text.toString().toInt()` – Bek Sep 11 '18 at 11:51
  • are you resolved? if not, post your xml and I'll try and help you on this. I noticed there is no answer marked as correct, so assuming it is not fixed yet. – Sam Oct 03 '18 at 13:50

2 Answers2

1

Try this

input1.text.toString().toInt() + input2.text.toString().toInt()

instead of

input1.toString().toInt() + input2.toString().toInt()

EDIT

output.setText((input1.text.toString().toInt() + input2.text.toString().toInt()).toString()) 

Use editText.setText()

Don't use output.text because editText.text expects an Editable, not a String.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

there were 2 problems

first being -> (unresolved symbol toInt())

I added these to app build gradle file & then cleaned project :

apply plugin: 'kotlin-kapt' 
android{
...
dataBinding {
    enabled = true
  }
}
dependencies{
....
 kapt 'com.android.databinding:compiler:3.1.4'
}

second being -> (this caused app crashes)

input1.text.toString().toInt() + input2.text.toString().toInt()

instead of

input1.toString().toInt() + input2.toString().toInt()
Anjani Mittal
  • 507
  • 1
  • 7
  • 19