即使通知管理器已正确初始化,也无法创建通知

Can't create notification even though notification manager is initialized correctly

我正在尝试使用 Android 中的 NotificationManager API 发送通知。下面是我的代码:

AndroidManifest.xml:

<uses-sdk
    android:minSdkVersion="23"
    android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
    <activity
        android:name="com.tabs.activity.Comments"
        android:configChanges="orientation|keyboardHidden"
        android:label="View Post"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateAlwaysHidden|adjustResize" >
    </activity>
    <service
        android:name="com.tabs.activity.NotificationService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
...

Comments.java:

public void createComment(){
    ...
    NotificationService notificationService = new NotificationService();
    notificationService.showNotification(getApplicationContext(), R.id.commenter_profile_photo, "User commented: Hi!", "User");
    ...
}

NotificationService.java:

public void showNotification(Context context, int commenterId, String messageBody, String commenter){
    NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Title")
            .setContentText(messageBody)
            .setTicker("New comment from " + commenter)
            .setSmallIcon(commenterId);

    Intent moreInfoIntent = new Intent(context, news_feed.class);

    //When the user clicks back, it doesn't look sloppy!
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addNextIntent(moreInfoIntent);

    //If the intent already exists, just update it and not create a new one
    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    //When the notification is actually clicked on
    notifiationBuilder.setContentIntent(pendingIntent);

    //Notification manager to notify of background event
    notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(1, notifiationBuilder.build());
}

显示的错误(堆栈跟踪):

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
                                                          at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:160)
                                                          at android.app.Notification$Builder.<init>(Notification.java:2287)
                                                          at android.support.v4.app.NotificationCompatApi21$Builder.<init>(NotificationCompatApi21.java:68)
                                                          at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:759)
                                                          at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1559)
                                                          at com.tabs.activity.NotificationService.showNotification(NotificationService.java:113)
                                                          at com.tabs.activity.Comments.createComment(Comments.java:146)

此代码的问题在于,无论出于何种原因,notificationManager.notify(1, notifiationBuilder.build()); 行总是生成 NullPointerException。我知道它们是什么,但我似乎无法解决这个问题,因为我不明白为什么我的上下文需要 getApplicationInfo()。当我从 Comments.java 传入它时,我的上下文应该已经有了,这让我很困惑。我只是不明白我怎么会错误地构建通知管理器。下面是给出的堆栈跟踪。任何帮助,将不胜感激。谢谢!

改变

NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(this)

NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(context)