当前位置: 首页 > 知识库问答 >
问题:

致命异常:main KotlinNullPointerException

尚安平
2023-03-14
 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 354 0-3540/com.example.ruiru.salestracker E/AndroidRuntime:致命异常:主进程:com.example.ruiru.salestracker,pid:3540 java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.ruiru.salestracker/com.example.ruiru.salestracker}:Kotlin.KotlinNullPointerException(位于Android.app.ActivityThread.PerformLaunchActivity(ActivityThread.java:2817)n在com.example.ruiru.salestracker.addsales.onCreate(addsales.kt:25)在android.app.activity.performCreate(activity.java:6975)在android.app.instrumentation.callactivityonCreate(instrumentation.java:1213)在android.app.activitythread.performLaunchActivity(activitythread.java:2770)在android.app.activitythread.handleLaunchActivity(activitythread.java:2892) 在android.app.activitythread.-wrap11(未知源:0) 在Android.app.ActivityThread$H.HandleMessage(ActivityThread.java:1593) 在android.os.handler.dispatchMessage(Handler.java:105) 在android.os.looper.loop(looper.java:164) 在android.app.activityThread.main(activityThread.java:6541) 在java.lang.Reflect.Method.Invoke(原生方法) 在com.android.internal.os.zygote$methodandargscaller.run(zygote.java:240) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:767)

共有1个答案

东方旺
2023-03-14

您似乎忘记初始化dateedittext。为什么不试试KotlinAndroid扩展插件。通过使用这个插件,您不需要初始化视图,而是可以直接使用它们的ID。对于您的情况:只需添加import

import kotlinx.android.synthetic.main.activity_add_sales.*

然后您现在可以删除:

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

您可以通过他们的ID访问他们。就像

date.setText("15-11-2017")
 类似资料:
  • 我是Android的初学者。我正在开发一个应用程序。我收到这个问题,我不知道怎么做。 这是我在logcat中收到的信息:

  • 请在这方面需要帮助。当我试图在模拟器上运行我的应用程序时,我的Logcat中出现了这些错误,我的UI崩溃了 logcat错误

  • 在v3 beta版SDK中,堆栈跟踪是: 病因是什么?

  • 这段代码在leapdroid中运行良好,但我得到了错误模拟器: 2020-06-25 13:43:36.936 14930-14930/?E/DatabaseLesson:runtime_flags中设置了未知位:0x8000 2020-06-25 13:43:54.808 149 30-14998/com.webstates.databaseLesson2 E/AndroidRuntime:致命

  • 当你点击btnChangePass,我得到错误。 我有一个错误: FATAL EXCEPTION:主进程:com.company.learn2,PID:18854java.lang.NullPointerExcture atcom.company.learn2。Heslo.change密码(Heslo.java:84)在com.company.learn2。Heslo.on点击(Heslo.jav