尝试从 class 调用它时 PendingIntent 为 null

PendingIntent null when trying to call it from a class

我正在构建一个闹钟,我得到了一个应该暂停闹钟的 pendingIntent。相同的代码在 Activity 中时效果很好。我改变了一些东西,现在它在不同的 class 中。我得到一个 nullpointerExecption。 (承包商从 activity.

获取上下文

贪睡功能:

public void settingNewIntentForSnooze() {
    PendingIntent alarmPendingIntent=null;
    Calendar calendar = Calendar.getInstance();
    // new alarm after the snooze.
    calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + snoozeTime);
    Intent snoozeIntent = new Intent(context, Alarm_Reciver.class);
    snoozeIntent.putExtra("click_status", true);
    //add that to try to solve the problem...
    snoozeIntent.setAction("Snooze");
    alarmPendingIntent = PendingIntent.getBroadcast(context, 0, snoozeIntent, alarmPendingIntent.FLAG_ONE_SHOT);
    //tells to phone to set the alarm
    alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmPendingIntent);
}

AlarmPage activity(创建变量的代码):

snooze= new Snooze(this,alarm_manager);

报警接收器:

public void onReceive(Context context, Intent intent) {
    Log.e("in alarm reciver","in alarm reciver");
    Intent service_intent= new Intent(context, RingtonePlayingService.class);//intent for the service ringtone playing
    boolean button_status = intent.getExtras().getBoolean("click_status");
    service_intent.putExtra("click_status", button_status);//passing button status
    context.startService(service_intent);//start the ringtone service
}

错误:

09-17 18:25:25.564 1563-1563/com.example.itay.newfrindlyalarm E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.itay.newfrindlyalarm, PID: 1563
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AlarmManager.set(int, long, android.app.PendingIntent)' on a null object reference
                                                                                at com.example.itay.newfrindlyalarm.Snooze.settingNewIntentForSnooze(Snooze.java:60)
                                                                                at com.example.itay.newfrindlyalarm.Snooze.operate(Snooze.java:44)
                                                                                at com.example.itay.newfrindlyalarm.AlarmPage.operateSnooze(AlarmPage.java:88)
                                                                                at com.example.itay.newfrindlyalarm.AlarmPage.onClick(AlarmPage.java:68)
                                                                                at android.view.View.performClick(View.java:6256)
                                                                                at android.view.View$PerformClick.run(View.java:24697)
                                                                                at android.os.Handler.handleCallback(Handler.java:789)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                at android.os.Looper.loop(Looper.java:164)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
enter code here

从报错信息来看,alarm_manager有没有可能是null?如果正确初始化 AlarmManager,错误会消失吗?