KOTLIN this in fragment - 类型不匹配

KOTLIN this in fragment - Type mismatch

我正在尝试制作通知 onclick 按钮,点击后它会发送通知。在该函数中,当我使用 'this' 时,它会给我一个类型不匹配错误。这是我的代码;

package com.example.myassignment.Fragments

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.example.myassignment.MainActivity
import com.example.myassignment.R
import com.tapadoo.alerter.Alerter
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.fragment_help.*
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_settings.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class SettingsFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }


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

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_settings, container, false)


    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        

        btnNotifications.setOnClickListener {
            Alerter.Companion.create(this) // <- Here's the error
                .setTitle("Title")
                .setText("Notification Sent")
                .setIcon(R.drawable.ic_baseline_circle_notifications_24)
        }
    }

我试图在创建函数中使用 'this',但它给我一个错误。我不知道如何解决这个问题。任何帮助将不胜感激。

谢谢

看起来您正在使用的 Alerter 库需要 Activity 而您有 this。在片段中,您可以使用 requireActivity() 来获取 activity 引用。