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设置为全局和静态,修复了此问题。无论如何,谢谢你。