致命异常:主要是KotlinNullPointerException

问题描述 投票:2回答:4

这是我第一次构建一个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进程:com.example.ruiru.salestracker,PID:3540 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.ruiru.salestracker / com.example.ruiru.salestracker.AddSales}:在android.app.ActivityThread.handleLaunchActivity(ActivityThread)的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)中的kotlin.KotlinNullPointerException。 java:2892)在Android.app.Handler.dispatchMessage(Handler.java:105)的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)的android.app.ActivityThread.-wrap11(未知来源:0) )在android.app.Looper.loop(Looper.java:164)的android.app.ActivityThread.main(ActivityThread.java:6541),位于com.android的java.lang.reflect.Method.invoke(Native Method)。 internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)引起:kotlin.KotlinNullPointerExceptio n位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)的android.app.Activity.performCreate(Activity.java:6975)的com.example.ruiru.salestracker.AddSales.onCreate(AddSales.kt:25)在android.app.A活动中的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)android.app.A活动时,我发现它位于android.app.A活动时,我发现Android.app.A活动。 ActivityThread $ H.handleMessage(ActivityThread.java:1593)位于android.app.AooT.Thread.main的android.os.Handler.dispatchMessage(Handler.java:105)android.os.Looper.loop(Looper.java:164) (ActivityThread.java:6541)位于com.android.internal.os的com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)的java.lang.reflect.Method.invoke(Native Method) .ZygoteInit.main(ZygoteInit.java:767)

android kotlin
4个回答
0
投票

好像你忘了初始化date editText。为什么不尝试Kotlin Android Extension plugin。通过使用此插件,您无需初始化视图,而是可以使用其ID直接使用它们。对于您的情况:只需添加导入

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")

1
投票

您的EditText为空。您可以初始化EditText。示例Kotlin代码,

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

    val date = findViewById<EditText>(R.id.editTextDate)
    date.apply {
        setText("15-11-2017")
        isEnabled = false
    }
  }

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

在使用这些变量之前,使用findViewById初始化它们或将它们绑定到xml视图。


0
投票

因为你的日期EditText是null。在使用editText之前初始化editText就像这样

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

然后设置日期的值

date?.setText("15-11-2017")
date?.isEnabled = false
© www.soinside.com 2019 - 2024. All rights reserved.