Issue
So I was trying to write a code for a pop-up date picker app. Don't mind the extra button and texts, for now, I'm gonna implement them later but this is my main:
```package com.example.agecalculator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.view.View
import android.widget.Button
import java.util.*
import android.app.DatePickerDialog
import android.widget.DatePicker
import java.text.SimpleDateFormat
class MainActivity : AppCompatActivity() {
var button_date: Button? = null
var textview_date: TextView? = null
var cal = Calendar.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textview_date = this.text_view_date_1
button_date = this.button_date_1
textview_date!!.text = "--/--/----"
val dateSetListener = object : DatePickerDialog.OnDateSetListener {
override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
dayOfMonth: Int) {
cal.set(Calendar.YEAR, year)
cal.set(Calendar.MONTH, monthOfYear)
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
updateDateInView()
}
}
button_date!!.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View) {
DatePickerDialog(this@MainActivity,
dateSetListener,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)).show()
}
})
}
private fun updateDateInView() {
val myFormat = "MM/dd/yyyy" // mention the format you need
val sdf = SimpleDateFormat(myFormat, Locale.US)
textview_date!!.text = sdf.format(cal.getTime())
}
}```
which gives me the two errors: "Unresolved reference: text_view_date_1" "Unresolved reference: button_date_1"
and this is my XML:
```<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
android:background="#3A3737"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="62dp"
android:layout_marginBottom="35dp"
android:text="@string/calculate_your"
android:textColor="#D53A3A"
android:textSize="28sp"
app:layout_constraintBottom_toTopOf="@+id/age_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/age_button"
android:layout_width="73dp"
android:layout_height="0dp"
android:layout_marginBottom="25dp"
android:backgroundTint="#E1D7D7"
android:text="@string/age"
android:textColor="#2196F3"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="43dp"
android:text="@string/in_years_months_days_hours"
android:textColor="#63C167"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/button_date_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/age_button" />
<Button
android:id="@+id/button_date_1"
android:layout_width="338dp"
android:layout_height="0dp"
android:layout_marginBottom="47dp"
android:text="@string/date_picker"
app:layout_constraintBottom_toTopOf="@+id/text_view_date_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/text_view_date_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="326dp"
android:text="@string/_23_03_2022"
android:textColor="#AC4DBC"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_date_1" />
<CalendarView
android:id="@+id/calendarView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="312dp"
android:background="#ECE2E2"
android:backgroundTint="#E8DCDC"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
I thought I would be able to assign "textview_date" to "text_view_date_1" and "button_date" to "button_date_1" since I changed their id in XML but apparently, I can't, please help.
Solution
You should initialize your views with findViewById
(synthetic is deprecated):
textview_date = findViewById<TextView>(R.id.text_view_date_1)
button_date = findViewById<Button>(R.id.button_date_1)
A better approach would be using view binding:
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// Access the views with
binding.textViewDate1
binding.buttonDate1
In this case, don't forget to add
android {
...
buildFeatures {
viewBinding true
}
}
In your module-level build.gradle
as stated in the official docs.
Answered By - Luca Pizzini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.