我不知道为什么这个应用程序正在关闭(因为数据绑定,setOnClickListener)

I don't know why this app is shutting down(because of databinding,setOnClickListener)

class day_1 : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private var _binding: FragmentDay1Binding? = null
private val binding get() = _binding!!
private var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    arguments?.let {
        param1 = it.getString(ARG_PARAM1)
        param2 = it.getString(ARG_PARAM2)


    }
    binding.startbutton.setOnClickListener {
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(activity, R.raw.days)
        }
        mediaPlayer?.start()
    }

    binding.pausebutton.setOnClickListener {
        if (mediaPlayer?.isPlaying == true) {
            mediaPlayer?.pause()
        } else {
            mediaPlayer?.start()
        }
    }

    binding.stopbutton.setOnClickListener {
        mediaPlayer?.stop()
        mediaPlayer = null
    }
     fun onStop() {
        super.onStop()
        mediaPlayer?.release()
        mediaPlayer = null
    }
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {

    // Inflate the layout for this fragment
    _binding = FragmentDay1Binding.inflate(inflater,container,false)
    return binding.root
}
companion object {
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment day_1.
     */
    // TODO: Rename and change types and number of parameters
    @JvmStatic
    fun newInstance(param1: String, param2: String) =
        day_1().apply {
            arguments = Bundle().apply {
                putString(ARG_PARAM1, param1)
                putString(ARG_PARAM2, param2)
            }
        }
}

以上是我的代码。如果我单击该按钮,应用程序将关闭。首先,我认为 Media Player 是个问题。但是没有 setOnClickListener 的应用程序没有问题。请高手帮帮我.... setOnClickListener 在Fragment 中有问题吗? 我可以修改这段代码吗?或者我重新启动项目?也许如果你知道为什么存在这个问题,请告诉我解决方法...

您可以在此处查看片段生命周期 https://developer.android.com/guide/fragments/lifecycle

您在 onCreate 方法中设置了点击侦听器,在片段中,视图是在 onCreateView 方法中创建的,因此您试图在视图创建之前访问它。因此,您需要在 binding/view 准备就绪后设置点击监听器,而不是 onCreate 方法,例如,您可以在 onCreateView 返回 binding.root 之前设置点击监听器。您也可以在 onViewCreated 方法中设置点击监听器。