我試圖使用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
傳遞給我時,我的上下文應該已經有了。我只是不明白我該如何錯誤地構建通知管理器。以下是給出的堆棧跟蹤。任何幫助,將不勝感激。謝謝!
好這個問題看起來非常眼熟...... – shmosel
@shmosel這是因爲它被標記爲重複時,我不認爲它應該已經做。我想這是因爲標題有點誤導,我想這是我的錯。 – user1871869
看起來像上下文沒有正確初始化?調試一次 – 7geeky