我正在尝试制作闹钟应用程序,我在菜单中提供了一个保存选项以保存新警报,每次我尝试保存应用程序崩溃时都是如此。 我把我的日志可以帮助我理解我的应用程序中的问题? 我的代码很少
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
/*checksonu case android.R.id.home: {
finish();
break;
} */
case R.id.action_save_alarm_details: {
updateModelFromLayout();
AlarmManagerHelper.cancelAlarms(this);
if (alarmDetails.id < 0) {
dbHelper.createAlarm(alarmDetails);
} else {
dbHelper.updateAlarm(alarmDetails);
}
AlarmManagerHelper.setAlarms(this);
setResult(RESULT_OK);
finish();
}
}
return super.onOptionsItemSelected(item);
}
public static void setAlarms(Context context) {
cancelAlarms(context);
AlarmDBHelper dbHelper = new AlarmDBHelper(context);
List<AlarmModel> alarms = dbHelper.getAlarms();
for (AlarmModel alarm : alarms) {
if (alarm.isEnabled) {
PendingIntent pIntent = createPendingIntent(context, alarm);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarm.timeHour);
calendar.set(Calendar.MINUTE, alarm.timeMinute);
calendar.set(Calendar.SECOND, 00);
//Find next time to set
final int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
final int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
final int nowMinute = Calendar.getInstance().get(Calendar.MINUTE);
boolean alarmSet = false;
//First check if it's later in the week
for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; ++dayOfWeek) {
if (alarm.getRepeatingDay(dayOfWeek - 1) && dayOfWeek >= nowDay &&
!(dayOfWeek == nowDay && alarm.timeHour < nowHour) &&
!(dayOfWeek == nowDay && alarm.timeHour == nowHour && alarm.timeMinute <= nowMinute)) {
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
setAlarm(context, calendar, pIntent);
alarmSet = true;
break;
}
}
//Else check if it's earlier in the week
if (!alarmSet) {
for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; ++dayOfWeek) {
if (alarm.getRepeatingDay(dayOfWeek - 1) && dayOfWeek <= nowDay && alarm.repeatWeekly) {
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.add(Calendar.WEEK_OF_YEAR, 1);
setAlarm(context, calendar, pIntent);
alarmSet = true;
break;
}
}
}