class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
**var X: EditText = findViewById(R.id.num12)
var Y: EditText = findViewById(R.id.num2)**
var B: Button = findViewById(R.id.btn1)
var vir: TextView = findViewById(R.id.virw)
var X1: Int = X.text.toString().toInt()
var Y1: Int = Y.text.toString().toInt()
B.setOnClickListener() {
vir.text = "add new ${addtwonumber(X1, Y1)}"
}
}
private fun addtwonumber(a:Int,b:Int):Int{
return a b
}
}
Error 2021-11-20 20:29:39.940 29733-29733/com.example.calibrate E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.calibrate, PID: 29733 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calibrate/com.example.calibrate.MainActivity}: java.lang.NumberFormatException: For input string: "" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3528) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 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:2152) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:250) at android.app.ActivityThread.main(ActivityThread.java:7886) 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:970) Caused by: java.lang.NumberFormatException: For input string: "" at java.lang.Integer.parseInt(Integer.java:627) at java.lang.Integer.parseInt(Integer.java:650) at com.example.calibrate.MainActivity.onCreate(MainActivity.kt:23) at android.app.Activity.performCreate(Activity.java:8108) at android.app.Activity.performCreate(Activity.java:8092) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3501) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 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:2152) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:250) at android.app.ActivityThread.main(ActivityThread.java:7886) 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:970) 2021-11-20 20:29:39.957 29733-29733/com.example.calibrate I/Process: Sending signal. PID: 29733 SIG: 9
CodePudding user response:
Looks at this line of the error:
java.lang.NumberFormatException: For input string: ""
You have an empty string (because your EditText is empty initially) which your are trying to convert to an Int
. That's why you are getting the NumberFormatException
.
You can fix the error by using String.toIntOrNull()
. This function returns null
instead of throwing an exception if the string cannot be converted to an Int
. Then you can maybe use the elvis operator(?:
) to use a default value for the null
Int
.
val X1: Int = X.text.toString().toIntOrNull() ?: 0 // Use zero if string does not represent an Int
On a side note, please use val
s instead of var
s for properties that are not going to change and also give better names to them (X, Y, X1, Y2 don't look good)
CodePudding user response:
At the beginning your EditTexts are empty, so the error is because it can't convert empty "" to integer, so you can check first if it is not empty or use toIntOrNull()
function, but you also want to get the x and y when the user clicks on the button not at the beginning so the right way is to move your code from onCreate to onClickListener body
B.setOnClickListener() {
val X1: Int = X.text.toString().toInt()
val Y1: Int = Y.text.toString().toInt()
vir.text = "add new ${addtwonumber(X1, Y1)}"
}