我正在使用一个简单的代码通过一个函数在Android中推送通知。此函数如下所示:
public void sendNotification(View view) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setContentTitle("My notification").setContentText("Hello World!");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager.notify().mNotificationManager.notify(001, mBuilder.build());
}}
这是我在一个网站上选择的示例函数。
一切正常,NotificationCompat.builder
不会返回任何错误。但是最后一行的第一个notify()
返回以下错误:Non-static method 'notify()' cannot be referenced from a static context
。
我真的不明白为什么。空白被放置在我的public class MainActivity extends AppCompatActivity {}
内的MainActivity.java
中
编辑:
解决方案是从mNotificationManager.notify(001, mBuilder.build());
中删除NotificationManager.notify().
发布于 2018-07-26 14:52:42
使用这个
mNotificationManager.notify(001, mBuilder.build());
而不是这个
NotificationManager.notify().mNotificationManager.notify(001, mBuilder.build());
发布于 2018-07-26 14:59:29
请参阅下面的解决方案。
public void sendNotification(View view) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setContentTitle("My notification").setContentText("Hello World!");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}}
https://stackoverflow.com/questions/51532639
复制相似问题