我一直在使用Kotlin开发一个小费计算器应用程序,我遇到了一个我似乎无法修复的错误,我已经绕过互联网,我无法找到明确的答案。
这是我的代码:
package com.jackjessop.testapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val billAmountTV: TextView = findViewById(R.id.billAmountText)
val tipPercentageTV: TextView = findViewById(R.id.tipPercentageText)
val billAmount = billAmountTV.text.toString().toInt()
val tipPercentage = tipPercentageTV.text.toString().toInt()
val percentage = tipPercentage / billAmount
val tipAmount = billAmount * percentage
val total = billAmount + tipAmount
val tipText: TextView = findViewById(R.id.tipText)
val calculateBtn: Button = findViewById(R.id.calculateButton)
calculateBtn.setOnClickListener {
tipText.text = total.toString()
}
}
}
这是错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jackjessop.testapp, PID: 30234
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jackjessop.testapp/com.jackjessop.testapp.MainActivity}: java.lang.NumberFormatException: For input string: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2779)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6364)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:533)
at java.lang.Integer.parseInt(Integer.java:556)
at com.jackjessop.testapp.MainActivity.onCreate(MainActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:6666)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6364)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)