无法从静态上下文中引用通知“notify()”
Notification `notify()` cannot be referenced from a static context
我正在使用一个简单的代码通过函数在 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
没有 return 任何错误。但是第一个notify()
最后一行return出现如下错误:Non-static method 'notify()' cannot be referenced from a static context
。
我真的不明白为什么。 void放在我的MainActivity.java
里面我的public class MainActivity extends AppCompatActivity {}
编辑:
解决方案是从 mNotificationManager.notify(001, mBuilder.build());
中删除 NotificationManager.notify().
使用这个
mNotificationManager.notify(001, mBuilder.build());
而不是这个
NotificationManager.notify().mNotificationManager.notify(001, mBuilder.build());
请参阅下面的解决方案。
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());
}}
我正在使用一个简单的代码通过函数在 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
没有 return 任何错误。但是第一个notify()
最后一行return出现如下错误:Non-static method 'notify()' cannot be referenced from a static context
。
我真的不明白为什么。 void放在我的MainActivity.java
里面我的public class MainActivity extends AppCompatActivity {}
编辑:
解决方案是从 mNotificationManager.notify(001, mBuilder.build());
NotificationManager.notify().
使用这个
mNotificationManager.notify(001, mBuilder.build());
而不是这个
NotificationManager.notify().mNotificationManager.notify(001, mBuilder.build());
请参阅下面的解决方案。
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());
}}