Android Контекст: несоответствие типов для этого - PullRequest
0 голосов
/ 02 мая 2020

Я создаю класс Notification с методом, который инициализирует Notification Builder. Первый параметр NotificationCompat.Builder является переменной Context. Я предоставляю this для параметра, но получаю следующую ошибку:

Несоответствие типов: Требуется: Контекст найден: Уведомление

Ошибка возникает в первой строке в коде моей функции setNotificationAtTime

Ниже мой код:

package com.example.notificationapp

import android.app.PendingIntent.getActivity
import android.content.Context
import androidx.core.app.NotificationCompat
import java.security.AccessController.getContext
import java.util.*

class Notification(title:String, description:String, date: Date) {
    // Attributes
    private var description:String = ""

    // Initialization
    init {
        // Download the constructor parameters into the object's variables
        this.description = description
    }

    // Getters
    fun getDescription():String {
        return description
    }

    // Setters
    fun setDescription(newDescription:String) {
        this.description = newDescription;
    }

    // Method to set the notification at a specific time
    fun setNotificationAtTime(time:Date) {
        val builder = NotificationCompat.Builder(this,
            "com.example.notificationapp")
    }
}

1 Ответ

0 голосов
/ 02 мая 2020

Чтение NotificationCompat.Builder

NotificationCompat.Builder (Context context, 
                String channelId)

Вы должны собрать Context Объект сначала

class Notification(context:Context,title:String, description:String, date: Date)

Затем передайте объект Context ниже

 fun setNotificationAtTime(time:Date) {
    val builder = NotificationCompat.Builder(context, "com.example.notificationapp").apply {

    }
  }
...