-2

hey everybody i'm french so please excuse my bad english . i'm a programmer who's learning kotlin and i'm making my first program with android studio so i want to make a button(add) who when pressed will add the content of two textboxes(imp1/imp2) and write it as a result in a third textbox(result) like a calculator but when i press the button it crashes and close the app

    add.setOnClickListener{
    result.text = (imp1.text as Int + imp2.text as Int) as CharSequence
                    }

2 Answers2

0

You can still use old java method

(Integer.parseInt(imp1.text.toString()) + Integer.parseInt(imp2.text.toString()))

Jawad Ahmed
  • 306
  • 1
  • 5
  • 9
  • E/AndroidRuntime: FATAL EXCEPTION: main – Med ali Damergi ReTr0 Nov 25 '18 at 07:57
  • `Process: com.example.dalid.myapplication, PID: 11447 android.content.res.Resources$NotFoundException: String resource ID #0x9 at android.content.res.Resources.getText(Resources.java:410) at android.content.res.HwResources.getText(HwResources.java:465) at android.widget.TextView.setText(TextView.java:5589) at com.example.dalid.myapplication.MainActivity$onCreate$1.onClick(MainActivity.kt:16) at android.view.View.performClick(View.java:6291) at android.view.View$PerformClick.run(View.java:24931)` – Med ali Damergi ReTr0 Nov 25 '18 at 08:00
  • ` at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)` **it may help** – Med ali Damergi ReTr0 Nov 25 '18 at 08:01
0

I guess imp1 and imp2 are EditTexts,
so imp1.text and imp2.text are of type Editable and not String.

If result is a TextView you must do this:

result.text = (imp1.text.toString().toInt() + imp2.text.toString().toInt()).toString()

or if result is an EditText:

result.setText((imp1.text.toString().toInt() + imp2.text.toString().toInt()).toString())
forpas
  • 160,666
  • 10
  • 38
  • 76