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

时间:2016-08-17 08:32:52

标签: java android notifications notificationmanager

我正在尝试使用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传递它时,我的上下文应该已经存在,这让我感到困惑。我只是看不出我是如何错误地构建通知管理器的。下面是给出的堆栈跟踪。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

更改

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

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