致命异常:主要KotlinNullPointerException

时间:2017-11-17 06:27:24

标签: android kotlin

这是我第一次构建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:致命异常:主要                                                                                 过程:com.example.ruiru.salestracker,PID:3540                                                                                 java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.ruiru.salestracker / com.example.ruiru.salestracker.AddSales}:   kotlin.KotlinNullPointerException                                                                                     在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)                                                                                     在   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)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在   com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                                                                                  引起:kotlin.KotlinNullPointerException                                                                                     在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)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在   com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

4 个答案:

答案 0 :(得分: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
    }
  }

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

因为您的日期EditText为null。使用editText之前初始化editText

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

然后设置日期值

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

答案 3 :(得分: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")