gpt4 book ai didi

android - 致命异常 : main KotlinNullPointerException

转载 作者:行者123 更新时间:2023-11-29 01:05:51 26 4
gpt4 key购买 nike

这是我第一次构建 Android 应用程序。但是,当我在我的虚拟设备上运行该应用程序时,它停止工作并不断崩溃。该错误说明了一些关于空指针异常的信息。这是我第一次使用 Kotlin,我用 Java 编写代码并更改为 Kotlin。

 override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_sales)

val date: EditText? = null
val changeDate: CheckBox? = null
val yes: RadioButton? = null
val no: RadioButton? = null

date!!.setText("15-11-2017")
date.isEnabled = false

val button2 = findViewById<View>(R.id.btnSubmit) as Button
button2.setOnClickListener {
var name = findViewById<View>(R.id.name) as EditText
var cost = findViewById<View>(R.id.cost) as EditText
val itemcost = Integer.parseInt(cost!!.text.toString())
var price = findViewById<View>(R.id.price) as EditText
val itemprice = Integer.parseInt(price!!.text.toString())
var qty = findViewById<View>(R.id.quantity) as EditText
var chgDate = false
var discount = false
val toast = Toast(applicationContext)
if (itemprice < itemcost) {
price!!.error = "Selling price cannot be lower than item price!"
} else {
if (changeDate!!.isChecked) {
chgDate = true
date.isEnabled = true
}
if (yes!!.isChecked) {
discount = true
}
}
Toast.makeText(this@AddSales, "Item Name: " + name!!.text + "\n" + "Cost: " + cost!!.text + "\n" + "Item Price: " + price!!.text + "\n" + "Quantity: " + qty!!.text + "Date: " + date + "Staff Discount: " + discount, Toast.LENGTH_SHORT).show()
}

}

错误是:

11-17 06:23:34.603 3540-3540/com.example.ruiru.salestracker E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ruiru.salestracker, PID: 3540 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ruiru.salestracker/com.example.ruiru.salestracker.AddSales}: kotlin.KotlinNullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: kotlin.KotlinNullPointerException at com.example.ruiru.salestracker.AddSales.onCreate(AddSales.kt:25) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

最佳答案

因为您的日期 EditText 为空。在使用 editText 之前像这样初始化 editText

var date = findViewById<View>(R.id.date) as? EditText

然后设置日期的值

date?.setText("15-11-2017")
date?.isEnabled = false

关于android - 致命异常 : main KotlinNullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47344537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com