estoy tratando de hacer una calculadora sencilla en kotlin y aunque no me marca ningun fallo de sintaxis al ejecutarla me salta este error:
> 07/05 19:45:05: Launching 'app' on Pixel 3a API 30.
> $ adb shell am start -n "com.example.calculadora/com.example.calculadora.MainActivity" -a
> android.intent.action.MAIN -c android.intent.category.LAUNCHER
> Connected to process 6232 on device 'emulator-5554'.
> Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the
> "Debugger" settings page.
> I/ple.calculador: The ClassLoaderContext is a special shared library.
> D/NetworkSecurityConfig: No Network Security Config specified, using platform default
> D/NetworkSecurityConfig: No Network Security Config specified, using platform default
> D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
> D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
> D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
> W/ple.calculador: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z
> (greylist, reflection, allowed)
> W/ple.calculador: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist,
> reflection, allowed)
> D/AndroidRuntime: Shutting down VM
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.example.calculadora, PID: 6232
> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculadora/com.example.calculadora.MainActivity}:
> java.lang.NumberFormatException: For input string: ""
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3356)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3500)
> at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
> at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
> at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
> at android.os.Handler.dispatchMessage(Handler.java:106)
> at android.os.Looper.loop(Looper.java:223)
> at android.app.ActivityThread.main(ActivityThread.java:7523)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
> Caused by: java.lang.NumberFormatException: For input string: ""
> at java.lang.Integer.parseInt(Integer.java:627)
> at java.lang.Integer.valueOf(Integer.java:801)
> at com.example.calculadora.MainActivity.onCreate(MainActivity.kt:38)
> at android.app.Activity.performCreate(Activity.java:7984)
> at android.app.Activity.performCreate(Activity.java:7973)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3329)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3500)
> at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
> at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
> at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
> at android.os.Handler.dispatchMessage(Handler.java:106)
> at android.os.Looper.loop(Looper.java:223)
> at android.app.ActivityThread.main(ActivityThread.java:7523)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
> I/Process: Sending signal. PID: 6232 SIG: 9
Agradezco mucho de antemano a quien me pueda ayudar de alguna forma o decirme como solucionar el problema. A continuación os dejo el código.
Kotlin:
package com.example.calculadora
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity(), View.OnClickListener {
private var TextView_Resultado: TextView? = null
private var EditText1: EditText? = null
private var EditText2: EditText? = null
private var boton1: Button? = null
private var boton2: Button? = null
private var boton3: Button? = null
private var boton4: Button? = null
private var boton5: Button? = null
private var num1: Int = 0
private var num2: Int = 0
var resultado: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
TextView_Resultado = findViewById(R.id.resultado) as TextView
EditText1 = findViewById(R.id.EditText1) as EditText
EditText2 = findViewById(R.id.EditText2) as EditText
num1 = Integer.valueOf(EditText1?.text.toString())
num2 = Integer.valueOf(EditText2?.text.toString())
boton1 = findViewById(R.id.button1) as Button
boton2 = findViewById(R.id.button2) as Button
boton3 = findViewById(R.id.button3) as Button
boton4 = findViewById(R.id.button4) as Button
boton5 = findViewById(R.id.button5) as Button
boton1!!.setOnClickListener(this)
boton2!!.setOnClickListener(this)
boton3!!.setOnClickListener(this)
boton4!!.setOnClickListener(this)
boton5!!.setOnClickListener(this)
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.button1 -> resultado = num1 + num2
R.id.button2 -> resultado = num1 - num2
R.id.button3 -> resultado = num1 * num2
R.id.button4 -> resultado = num1 / num2
R.id.button5 -> TextView_Resultado?.text = resultado.toString()
}
}
}