日期选择器对话不工作。单击确定按钮后应用程序崩溃,

Date picker dialogue is not working. App is crashing after clicking the OK button,

选择日期后单击日期对话框的确定​​按钮应用程序崩溃!! 代码中没有显示错误。有什么问题吗?

// MainActivity.kt 文件。

package com.nandini.android.dobcalc

import android.app.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity() {

   private var dateTv : TextView?=null
    private var minTv : TextView?=null

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

        val btnDatePicker : Button = findViewById(R.id.btnDatePicker)
        dateTv=findViewById(R.id.date_tv)
        minTv=findViewById(R.id.min_tv)

        btnDatePicker.setOnClickListener {
         datePicker()
        }

    }

    private fun datePicker ()
    {
        val myCalender = Calendar.getInstance()
        val year = myCalender.get(Calendar.YEAR)
        val month = myCalender.get(Calendar.MONTH)
        val day = myCalender.get(Calendar.DAY_OF_MONTH)

        val dpd = DatePickerDialog(this,
            { _, selectedYear, selectedMonth, selectedDay ->
                Toast.makeText(this,"Year was $selectedYear , ${selectedMonth+1}'s $selectedDay day.",Toast.LENGTH_SHORT).show()

                val selectedDate="$selectedDay . ${selectedMonth+1} . $selectedYear"
                dateTv?.text = selectedDate
                val sdf= SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH)
                val theDate=sdf.parse(selectedDate)

                theDate?.let {

                    val selectedDateInMin=theDate.time / 60000
                    val currentDate=sdf.parse(sdf.format(System.currentTimeMillis()))

                    currentDate?.let {

                        val currentDateInMin=currentDate.time/60000
                        val differenceInMin = currentDateInMin-selectedDateInMin
                        minTv?.text=differenceInMin.toString()
                    }

                } },year,month,day)
            dpd.datePicker.maxDate=System.currentTimeMillis()-86400000
            dpd.show()


    }
}

// activity_main.xml 父文件具有线性布局。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/bgColor"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Calculate your"
        android:textSize="25sp"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:layout_marginTop="16dp"
        android:textColor="@color/txtColor" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Age"
        android:textSize="25sp"
        android:padding="10dp"
        android:background="@color/txtBg"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:layout_marginTop="16dp"
        android:textColor="@color/txtColor" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="In Minutes"
        android:textSize="25sp"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:layout_marginTop="16dp"
        android:textColor="@color/txtColor" />

    <Button
        android:id="@+id/btnDatePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#D9EADD"
        android:layout_margin="16dp"
        android:text="Select Date"
        android:textStyle="bold"
        android:textSize="18sp"
        android:textColor="@color/txtBg"/>

    <TextView
        android:id="@+id/date_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00.00.00"
        android:layout_marginTop="16dp"
        android:textAllCaps="true"
        android:textColor="@color/txtColor"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="In Minutes"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textAllCaps="false"
        android:layout_marginTop="8dp"
        android:textColor="#98B0A8" />

    <TextView
        android:id="@+id/min_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="35sp"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:layout_marginTop="25dp"
        android:textColor="@color/txtColor" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="In Minutes"
        android:textSize="25sp"
        android:textStyle="bold"
        android:textAllCaps="false"
        android:layout_marginTop="8dp"
        android:textColor="#98B0A8" />

</LinearLayout>

添加了日期选择器,使用了日历 class 并将日期作为输入并转换当前日期和所选日期的差异(以分钟为单位)。

确切的错误在此代码块中。

val selectedDate="$selectedDay . ${selectedMonth+1} . $selectedYear"
dateTv?.text = selectedDate
val sdf= SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH)
val theDate=sdf.parse(selectedDate)

您已将 SimpleDateFormat 定义为 dd/MM/yyyy,但提供了错误的日期格式作为 sdf 的输入。 selectedDate 变量包含此格式 dd . MM . yyyy,因此会引发错误。

尝试为 selectedDate 变量分配正确的日期格式。

val selectedDate = "$selectedDay/${selectedMonth+1}/$selectedYear"