致命异常:主要KotlinNullPointerException [英] FATAL EXCEPTION: main KotlinNullPointerException

查看:867
本文介绍了致命异常:主要KotlinNullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my first time building an android application. However when i run the app on my Virtual Device, it stopped working and keeps crashing. The error says something about null pointer exception. This is my first time using Kotlin and I coded in Java and changed to 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:致命异常:main 流程: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) 在java.lang.reflect.Method.invoke(本机方法) 在 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) 在java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

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)

推荐答案

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

It seems like you forgot to initialize date editText. Why not to try Kotlin Android Extension plugin. By using this plugin you don't need to initialize the views instead you can directly use them using their id. For your case: Just add import

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

然后,您现在可以删除您的:

Then you can now remove your:

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

,您可以通过其ID访问它们.喜欢

and you can access them by their ids. like

date.setText("15-11-2017")

这篇关于致命异常:主要KotlinNullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆