Search code examples
androidkotlincrashdatepickerdialog

My app is closing after i pic the date , can anyone help me , i have attatched the code


This is my code and it crashes after i select the date in date picker dialog

bdaybutton.setOnClickListener { view -\\\>

    val c = Calendar.getInstance()
    val cyear = c.get(Calendar.YEAR)
    val cmonth = c.get(Calendar.MONTH)
    val cday = c.get(Calendar.DAY_OF_MONTH)

    val abc = DatePickerDialog(  this , DatePickerDialog.OnDateSetListener
    { view , year, month, day -\\\>

        flag2 = true
        year2 = year
        val selecteddate = "${day/month+1/year}"
        bdaydatetext.text = "${day/month+1/year}"
        val sdf = java.text.SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
        val date2 = sdf.parse(selecteddate)
        time2 = date2.time.toInt()

    }, cyear, cmonth, cday)

    abc.show()

]

}

The error in the imge appears when after selecting the date


Solution

  • "${day/month+1/year}"
    

    This computes the expression inside the {} and converts it to a string. Looks like you wanted something like

    "${day}/${month+1}/${year}"
    

    instead there.

    Better yet, skip the string conversion step altogether and use something like Java 8 LocalDate or LocalDateTime directly instead.