错误:getSystemServise未解析引用-kotlin android studio

错误:getSystemServise未解析引用-kotlin android studio,android,push-notification,kotlin,android-service,Android,Push Notification,Kotlin,Android Service,我是kotlin的新手,我尝试在我的应用程序中推送通知。 不幸的是,我遇到了这个错误:“getSystemServise未解析引用” 这是我的代码: val intent = Intent() val pendingIntent = PendingIntent.getActivity(this.activity,0,intent,0) val notification = Notification.Builder(this.activity)

我是kotlin的新手,我尝试在我的应用程序中推送通知。 不幸的是,我遇到了这个错误:“getSystemServise未解析引用” 这是我的代码:

        val intent = Intent()
        val pendingIntent = PendingIntent.getActivity(this.activity,0,intent,0)
        val notification = Notification.Builder(this.activity)
                .setContentTitle("time is:")
                .setContentText("text")
                .setSmallIcon(R.drawable.notification_icon_background)
                .setContentIntent(pendingIntent)

        val nm:NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(0,notification.build())

感谢您的帮助

我假设您的代码在
片段中
,这就是您多次引用
此.activity
的原因。作为类上的一个方法,您需要一个
上下文
实例来调用它。您可以通过使用或已使用的(因为an也是
上下文
)来获取一个

使用Kotlin的属性访问语法,这看起来像:

// either of these
this.activity.getSystemService(...)
this.context.getSystemService(...)
甚至只是:

// either of these
activity.getSystemService(...)
context.getSystemService(...)

我也面临着同样的问题。
我用前面的答案得到了部分解,但还有一件事你必须提到,即变量是否可为null,并相应地放置运算符:

val downloadManager = context?.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?

这是活动代码还是…?这是片段代码