这个 Button.onClickListener 有什么问题,似乎不起作用

What is wrong with this Button.onClickListener which seems that is not working

我正在尝试实现 here 中的基本示例,并得到以下代码:

class MainActivity : AppCompatActivity() {
    private lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater)
        imageView = binding.imageView
        binding.button1.setOnClickListener {
            this.dispatchTakePictureIntent()
        }
        setContentView(R.layout.activity_main)
    }

    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        try {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        } catch (e: ActivityNotFoundException) {
            println(e.stackTrace)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            val imageBitmap = data?.extras?.get("data") as Bitmap
            imageView.setImageBitmap(imageBitmap)
        }
    }

}

代码生成,我可以在我的 phone 中部署和 运行 应用程序,但是当我点击我设置点击监听器的按钮时,没有任何反应。谁能告诉我这里出了什么问题?

ps.: 这里是与activity相关的布局,如果需要的话:

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="348dp"
        android:layout_height="177dp"
        android:layout_marginStart="38dp"
        android:layout_marginTop="151dp"
        android:layout_marginEnd="25dp"
        android:background="#0091EA"
        android:contentDescription="@string/todo"
        app:layout_constraintBottom_toTopOf="@+id/button4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="158dp"
        android:layout_marginTop="100dp"
        android:layout_marginEnd="151dp"
        android:layout_marginBottom="264dp"
        android:text="@string/take_pic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button4" />

</androidx.constraintlayout.widget.ConstraintLayout>

您已经通过视图绑定扩充了一个布局,并且该布局中的按钮是您添加了点击侦听器的按钮。

但是,您从未使用过您扩充的布局。相反,你打电话给

setContentView(R.layout.activity_main)

它会扩充布局的新副本,但与您使用视图绑定扩充的实例无关。将此行替换为

setContentView(binding.root)

您是否授予应用程序读取存储的权限?

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />