Genero dos números aleatorios: NumU y NumD, con un rango y el método aleatorio, todo bien y tal, pero al iniciar la activity, esta se cierra. Viendo en el log cat, arroja esto:
2021-03-28 22:02:34.438 13208-13208/com.danysoftware.practmate E/AndroidRuntime: FATAL EXCEPTION: main Process: com.danysoftware.practmate, PID: 13208 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.danysoftware.practmate/com.danysoftware.practmate.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x5b at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.main(ActivityThread.java:8178) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5b at android.content.res.Resources.getText(Resources.java:413) at android.widget.TextView.setText(TextView.java:6427) at com.danysoftware.practmate.MainActivity.onCreate(MainActivity.kt:46) at android.app.Activity.performCreate(Activity.java:8086) at android.app.Activity.performCreate(Activity.java:8074) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
me dice que en linea 46 de MainActivity (Que para ustedes seria la que esta comentada en mayusculas), veamos que hay en ella:
class MainActivity : AppCompatActivity() {
var operando:Char = 'a'
var txtOperando: TextView? = null
var txtNumUno: TextView? = null
var txtNumDos: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*
var Puntuacion: Int = 300*/
txtOperando = findViewById(R.id.txtOp)
txtNumUno = findViewById(R.id.txtNumU)
txtNumDos = findViewById(R.id.txtNumD)
var NumU:Int = 0
var NumD:Int = 0
txtNumUno?.setText("")
txtNumDos?.setText("")
val operacion = intent.getStringExtra("operacion")
/* Toast.makeText(this, operacion, Toast.LENGTH_SHORT).show()*/
NumU = (1..100).random()
NumD = (1..100).random()
if(operacion.equals("suma")){
operando = '+'
txtOperando?.setText(operando.toString())
txtNumUno?.setText(NumU)//ESTA ES LA LINEA 46
txtNumDos?.setText(NumD)
}else{
if (operacion.equals("resta")){
operando = '-'
txtOperando?.setText(operando.toString())
txtNumUno?.setText(NumU)
txtNumDos?.setText(NumD)
}else{
if (operacion.equals("multiplicacion")){
operando = '*'
txtOperando?.setText(operando.toString())
txtNumUno?.setText(NumU)
txtNumDos?.setText(NumD)
}else{
if (operacion.equals("division")){
operando = '/'
txtOperando?.setText(operando.toString())
txtNumUno?.setText(NumU)
txtNumDos?.setText(NumD)
}
}
}
}
var editText = findViewById<EditText>(R.id.edtxtResp)
var textoRec = editText.text
var Primero:Boolean = true
val btnComprobar = findViewById<Button>(R.id.btnComprobar)
btnComprobar.setText("Empezar!")
btnComprobar.setOnClickListener(View.OnClickListener {
var resPar:Int = NumU + NumD
if(resPar.toString().equals(textoRec)){
Toast.makeText(this,"Correcto +100",Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this,"Inorrecto -100",Toast.LENGTH_SHORT).show()
}})}}
Porque se cierra la app y me da el error, si aparentemente todo esta bien? Que solución le puedo dar?