【问题标题】:Strange crash when starting notification启动通知时奇怪的崩溃
【发布时间】:2018-07-24 01:20:33
【问题描述】:

请不要将此标记为某些通用空指针异常问题的重复项,如您所见,异常并不直接在我的代码中,它在 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() 可能返回空值。我仍然无法重现这一点,但考虑到异常的消息,它会有点意义。我只是不知道为什么build() 方法无法创建通知对象。

【问题讨论】:

  • 您将 null 传递给通知管理器。使用调试器单步执行您的代码并查找空值,特别是查看这一行 Notification notification = builder.build(); 并查看您的通知是否真的生成了。
  • 您在查看异常消息时可能是正确的。为什么它会为空? build() 方法有时返回 null 是否正常?当我自己单步执行代码时,我永远不会得到 null。
  • 我记得 singleTask 应用程序存在一些问题,导致 NotificationBuilder 出现奇怪的行为。但是,如果您没有作为 singleTask 运行,则不必担心...如果它不为 null,则它可能会被回收。当您收到该错误时,通知是否被取消?
  • 我也想知道这是否是权限问题。您是否尝试过测试以查看当用户拒绝来自应用的通知时是否收到错误消息?
  • 其实我正在运行 SingleTask。我将通过拒绝通知来进行测试。

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


【解决方案1】:

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

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

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

【讨论】:

  • 我会尝试并在今天发布一个测试版。谢谢。
  • 似乎已经消失,所以我将其标记为正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-16
  • 2019-03-21
  • 1970-01-01
相关资源
最近更新 更多