Im bulding一個鬧鐘,我得到一個pendingIntent,應該使鬧鐘暫停。當它在活動中時,相同的代碼工作得很好。我改變了一些東西,現在改成了另外一類。我得到一個nullpointerExecption。 (承包商獲得從活動上下文PendingIntent null當試圖從類中調用它
暫停功能:
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活性(即外幣的變量的代碼):
snooze= new Snooze(this,alarm_manager);
AlarmReciver:
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$3.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
爲什麼你要在第一行定義'alarmPendingInten = null',但是當你可以'getBroadcast'後再覆蓋它呢?這是不好的風格:) –
'settingNewIntentForSnooze'方法中'context'的值是什麼?它有很好的價值嗎? –
請添加您的錯誤信息。這是很難調試代碼沒有看到錯誤:) –