FATAL EXCEPTION: main KotlinNullPointerException

后端 未结 4 463
悲哀的现实
悲哀的现实 2021-01-20 10:46

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

相关标签:
4条回答
  • 2021-01-20 11:00

    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
    

    and you can access them by their ids. like

    date.setText("15-11-2017")
    
    0 讨论(0)
  • 2021-01-20 11:05
    val date: EditText? = null
    val changeDate: CheckBox? = null
    val yes: RadioButton? = null
    val no: RadioButton? = null
    

    Before using these variables, initialize them with findViewById or bind them to the xml view.

    0 讨论(0)
  • 2021-01-20 11:16

    Your EditText is null. You can initialize your EditText. Example Kotlin code,

     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 讨论(0)
  • 2021-01-20 11:19

    Because your date EditText is null.Before use editText initialise editText like this

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

    then set the value of date

    date?.setText("15-11-2017")
    date?.isEnabled = false
    
    0 讨论(0)
提交回复
热议问题