Java 向NotificationManagerCompat发送可疑转换
我正在尝试使用以下代码从服务创建通知:Java 向NotificationManagerCompat发送可疑转换,java,android,Java,Android,我正在尝试使用以下代码从服务创建通知: NotificationCompat.Builder notif=new NotificationCompat.Builder(this,"ID1").setContentTitle("HI").setContentText("THERE"); NotificationManagerCompat man=(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE); man.notif
NotificationCompat.Builder notif=new NotificationCompat.Builder(this,"ID1").setContentTitle("HI").setContentText("THERE");
NotificationManagerCompat man=(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE);
man.notify("tag",notif);
但是我得到了这个错误:对于
通知服务
,可疑的强制转换到通知管理器公司
:应为通知管理器
我还尝试将NotificationManager compat
更改为NotificationManager
,这给了我以下错误:“android.app.NotificationManager”中的“notify(int,android.app.Notification)”不能应用于“(java.lang.String,android.support.v4.app.NotificationCompat.Builder)” 另外,将
NotificationCompat.Builder
更改为Notification.Builder
需要将API版本更改为26,我不打算这样做。非常感谢您的帮助。引用: 要使用此类,请从(上下文)调用静态函数
,以获取NotificationManagerCompat
对象,然后调用其方法之一来发布或取消通知
因此,替换:
NotificationManagerCompat man=(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE);
与:
引述:
要使用此类,请从(上下文)
调用静态函数,以获取NotificationManagerCompat
对象,然后调用其方法之一来发布或取消通知
因此,替换:
NotificationManagerCompat man=(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE);
与:
使用此示例使用notificationmanager发送通知
private void sendNotification() {
// Send notifications to watch
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(Integer.toString(mStepsCount) + " " +
getResources().getString(R.string.steps))
.setSmallIcon(R.mipmap.ic_notification_fitwatch);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getApplicationContext());
// Build the notification and issues it with notification manager.
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
使用此示例使用notificationmanager发送通知
private void sendNotification() {
// Send notifications to watch
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(Integer.toString(mStepsCount) + " " +
getResources().getString(R.string.steps))
.setSmallIcon(R.mipmap.ic_notification_fitwatch);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getApplicationContext());
// Build the notification and issues it with notification manager.
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
现在我得到:java.lang.NullPointerException:在尝试执行此操作时,尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”line@Abol_Fa:假定您在字段初始值设定项中有此行。在super.onCreate()
之后调用NotificationManagerCompat.from()
的onCreate()内部。下面是我的onCreate方法:public void onCreate(){super.onCreate();man=NotificationManagerCompat.from(this);}
调用man.notify时,我得到了java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()”
只是为了添加:man.notify()正在从服务实现的接口调用。@Abol\u Fa:我建议您单独问一个堆栈溢出问题,在这里您可以提供最新的堆栈溢出问题。这将包括完整的堆栈跟踪和当前代码。通过将notificationmanagercompat和notificationcompat.builder设置为全局和静态,修复了此问题。无论如何,谢谢。现在我得到:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”
执行此操作时line@Abol_Fa:假定您在字段初始值设定项中有此行。在super.onCreate()
之后调用NotificationManagerCompat.from()
的onCreate()内部。下面是我的onCreate方法:public void onCreate(){super.onCreate();man=NotificationManagerCompat.from(this);}
调用man.notify时,我得到了java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()”
只是为了添加:man.notify()正在从服务实现的接口调用。@Abol\u Fa:我建议您单独问一个堆栈溢出问题,在这里您可以提供最新的堆栈溢出问题。这将包括完整的堆栈跟踪和当前代码。通过将notificationmanagercompat和notificationcompat.builder设置为全局和静态,修复了此问题。无论如何,谢谢你。