首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >KOTLIN this in fragment - Type mismatch

KOTLIN this in fragment - Type mismatch
EN

Stack Overflow用户
提问于 2021-02-18 21:40:48
回答 1查看 63关注 0票数 0

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

代码语言:javascript
复制
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)
        }
    }

我试图在create函数中使用'this‘,但它给我一个错误。我想不出怎么解决这个问题。任何帮助都将不胜感激。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-18 21:52:36

看起来您正在使用的Alerter库需要一个包含thisActivity。在片段中,您可以使用requireActivity()来获取活动引用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66261157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档