App is crashing on clicking the OK button of date dialogue box after selecting the date !! There're no errors showing in the code. What's the problem ?
// MainActivity.kt file.
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 file having linear layout as parent.
<?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>
Added date picker , used Calendar class and take dates as input & convert the difference of current date and selected date in minutes.
CodePudding user response:
The exact error is in this code block.
val selectedDate="$selectedDay . ${selectedMonth 1} . $selectedYear"
dateTv?.text = selectedDate
val sdf= SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH)
val theDate=sdf.parse(selectedDate)
You have defined SimpleDateFormat as dd/MM/yyyy, but providing wrong date format as input to sdf.
selectedDate variable contain this format dd . MM . yyyy, so it's throwing error.
Try to assign selectedDate variable with the correct date format.
val selectedDate = "$selectedDay/${selectedMonth 1}/$selectedYear"


