Kotlin Callender 弹出未解决的引用错误

Kotlin Callender Pop-up Unresolved Reference Error

所以我试图为弹出式日期选择器应用程序编写代码。不要介意额外的按钮和文本,现在,我稍后会实现它们,但这是我的主要内容:

```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())
    }

}```

这给了我两个错误: “未解决的参考:text_view_date_1” “未解决的参考:button_date_1”

这是我的 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>

我想我可以将“textview_date”分配给“text_view_date_1”,将“button_date”分配给“button_date_1”,因为我在XML但显然,我不能,请帮忙。

您应该使用 findViewById 初始化您的视图(合成已弃用):

textview_date = findViewById<TextView>(R.id.text_view_date_1)
button_date = findViewById<Button>(R.id.button_date_1)

更好的方法是使用视图绑定:

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 

在这种情况下,不要忘记添加

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

在你的module-levelbuild.gradle中如官方所述docs