启动通知时发生奇怪的崩溃

Strange crash when starting notification

请不要将此标记为某些通用空指针异常问题的副本,如您所见,异常不直接在我的代码中,它在 Android 通知的深处 类。

我自己从来没有真正复制过这个,我只在 Crashlytics 上得到它。发生在 Android 7 和 8 多个制造商。它看起来类似于 Android.app.Notification.extras null 但那里没有答案,它们的堆栈跟踪略有不同。这是例外情况:

 java.lang.NullPointerException: Attempt to read from field 'android.os.Bundle android.app.Notification.extras' on a null object reference
           at android.app.Notification.addFieldsFromContext(Notification.java:2439)
           at android.app.Notification.addFieldsFromContext(Notification.java:2432)
           at android.app.NotificationManager.notifyAsUser(NotificationManager.java:300)
           at android.app.NotificationManager.notify(NotificationManager.java:289)
           at android.app.NotificationManager.notify(NotificationManager.java:273)
           at mypackageMyService.notify(MyService.java:812)
           at mypackageMyService.createNotificationAfterCheckOfStatus(MyService.java:1040)
           at mypackageMyService.onStartCommand(MyService.java:173)
           at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3474)
           at android.app.ActivityThread.-wrap20(Unknown Source)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
           at android.os.Handler.dispatchMessage(Handler.java:106)
           at android.os.Looper.loop(Looper.java:164)
           at android.app.ActivityThread.main(ActivityThread.java:6494)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是我的代码:

Intent intent = new Intent(this, SomeClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, PENDING_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
NotificationCompat.Builder builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_stat_notification_icon)
                .setOngoing(true)
                .setAutoCancel(false)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_LOW);
builder.setContentText(getString(R.string.please_wait));
if (oreoOrHigher) {
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
            if (notificationChannel == null) {

      /* Create or update. */
                CharSequence channelName = getString(R.string.my_channel_label);
                int importance = NotificationManager.IMPORTANCE_LOW;
                notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, importance);
                notificationChannel.enableLights(false);
                notificationChannel.enableVibration(false);
                notificationManager.createNotificationChannel(notificationChannel);
            }
            builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}
builder.setOngoing(false);
//fake extra to see if it fixes null pointer
Bundle fakeExtra = new Bundle();
fakeExtra.putString("test", "test");
builder.addExtras(fakeExtra);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
startForeground(NOTIFICATION_ID, notification);

我什至尝试添加一个假的额外捆绑包,但没有帮助。

我做错了什么?

谢谢。

编辑:有人指出 builder.build() 可能返回 null。我仍然无法重现它,但考虑到异常消息,它会有点意义。我只是不知道为什么 build() 方法会创建通知对象失败。

尝试在创建通知时将 this 替换为 getApplicationContext()

NotificationCompat.Builder builder = NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID)

您的通知不是 null,正如您在堆栈跟踪中看到的那样,您已成功调用通知实例。但是 Notification.addFieldsFromContext 是个例外,所以看起来 ContextBundlenull.