Android预定通知错误

时间:2016-04-04 15:27:48

标签: java android android-notifications

我正在使用此代码进行“预定通知”,因此即使应用程序不能正常工作,也会显示通知。

但是当我使用手机时(在通话过程中,使用YT应用程序时等),我的Nexus发生了一些Android-M随机出现的崩溃事件。

制作预定通知是好还是坏?

private void scheduleNotification(Notification notification, int delay) {
    Intent notificationIntent = new Intent(mCtx, NotificationPublisher.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, (int) System.currentTimeMillis());
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(mCtx, (int) System.currentTimeMillis(), notificationIntent, PendingIntent.FLAG_ONE_SHOT);
    long futureInMillis = System.currentTimeMillis() + delay;
    AlarmManager alarmManager = (AlarmManager) mCtx.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, pendingIntent);
}

编辑:我实际上遇到了异常,但仍然不知道如何修复它。

Process: com.ko.makemyhaircut, PID: 2184
                                                 java.lang.RuntimeException: Unable to start receiver com.ko.makemyhaircut.model.NotificationPublisher: java.lang.NullPointerException: Attempt to read from field 'android.net.Uri android.app.Notification.sound' on a null object reference
                                                     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
                                                     at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:148)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                  Caused by: java.lang.NullPointerException: Attempt to read from field 'android.net.Uri android.app.Notification.sound' on a null object reference
                                                     at android.app.NotificationManager.notify(NotificationManager.java:213)
                                                     at android.app.NotificationManager.notify(NotificationManager.java:194)
                                                     at com.ko.makemyhaircut.model.NotificationPublisher.onReceive(NotificationPublisher.java:17)
                                                     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
                                                     at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:148) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:1)

我有一个类似的问题,事实证明我正在使用一个全局变量作为上下文,结果是null。我可以看到你也在做同样的事情。你的变量

mCtx

包含上下文。如果为null,那么您将得到该错误。 将上下文保存在全局变量中永远不是一个好主意。