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

时间:2018-02-13 15:57:31

标签: android android-notifications android-7.0-nougat android-8.0-oreo

请不要将此标记为某些通用空指针异常问题的副本,正如您所看到的,异常不是直接在我的代码中,而是在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()方法无法创建通知对象。

1 个答案:

答案 0 :(得分:2)

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

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

您的通知不是null,您可以在堆栈跟踪中看到成功调用通知实例。但例外情况在Notification.addFieldsFromContext,因此Bundle的{​​{1}}似乎是Context