我试图通过使用getSystemService从片段中使用一个intentService来设置发送intent的警报。但是由于null对象引用,我在getsystemservice上得到一个nullpointer异常。也请帮助我找到一个方法来传递时间设定为主要活动。
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
public int hour,minute,id;
private AlarmManager alarmstart;
private AlarmManager alarmstop;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
alarmstart = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmstop = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
return new TimePickerDialog(getActivity(),this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void identify(int identity){
id=identity;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
Calendar calendar = Calendar.getInstance();
if(getActivity()!=null){
Intent start =new Intent(getActivity(),starter.class);
PendingIntent pi=PendingIntent.getService(getActivity(),0,start,0);
Intent stop =new Intent(getActivity(),stopper.class);
PendingIntent pi2=PendingIntent.getService(getActivity(),0,stop,0);
switch (id) {
case 1:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
case 2:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi2);
case 3:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
case 4:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi2);
case 5:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
case 6:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi2);
case 7:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
case 8:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi2);
case 9:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
case 10:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi2);
case 11:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
case 12:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi2);
case 13:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
case 14:
calendar.setTimeInMillis(0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmstart.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi2);
}
}
}
}