为什么不兼容的类型?

Ruc*_*nia 0 java algorithm android object shared-libraries

我想startForeground(int, id, notification)在我的服务中,但为此,我需要一个通知参数.在过去的3个小时里,我一直在尝试创建该通知,但它不起作用:

 Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("Notification title")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("Notification text");
Run Code Online (Sandbox Code Playgroud)

这是错误:

在此输入图像描述

我试过删除并替换我的所有导入,但没有任何作用.我怎样才能解决这个问题?

谢谢,

Ruchir

Moh*_*uag 6

您需要.build()在通知构建器上调用以获取通知:

 Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("Notification title")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("Notification text") // <---- NotificationCompat.Builder
                .build(); // <-------- 
Run Code Online (Sandbox Code Playgroud)

你试图分配NotificationCompat.Builder一个Notification.