我正在创建一个闹钟问题,每次我删除列表视图的最后一项时,我的应用程序崩溃,即使在谷歌搜索和敲击我的头脑后,我也无法弄清楚如何修复。
这是我的SQLitehelper代码
public int deleteAlarm(long id) {
return getWritableDatabase().delete(Alarm.TABLE_NAME,
Alarm._ID + " = ?", new String[] { String.valueOf(id) });
}
AlarmManagerHelper.setAlarms
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);
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;
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;
}
}
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;
}
}
}
}
}
}
感谢任何帮助。
logcat的
05-24 02:47:33.640: E/AndroidRuntime(819): FATAL EXCEPTION: main
05-24 02:47:33.640: E/AndroidRuntime(819): Process: com.b.alarmclock, PID: 819
05-24 02:47:33.640: E/AndroidRuntime(819): java.lang.NullPointerException
05-24 02:47:33.640: E/AndroidRuntime(819): at com.b.alarmclock.alarm.AlarmManagerHelper.setAlarms(AlarmManagerHelper.java:31)
05-24 02:47:33.640: E/AndroidRuntime(819): at com.b.alarmclock.alarm.AlarmClock.deleteAlarm(AlarmClock.java:64)
05-24 02:47:33.640: E/AndroidRuntime(819): at com.b.alarmclock.alarm.AlarmClock$AlarmListAdapter$2.onLongClick(AlarmClock.java:178)
05-24 02:47:33.640: E/AndroidRuntime(819): at android.view.View.performLongClick(View.java:4474)
05-24 02:47:33.640: E/AndroidRuntime(819): at android.view.View$CheckForLongPress.run(View.java:18401)
05-24 02:47:33.640: E/AndroidRuntime(819): at android.os.Handler.handleCallback(Handler.java:733)
05-24 02:47:33.640: E/AndroidRuntime(819): at android.os.Handler.dispatchMessage(Handler.java:95)
05-24 02:47:33.640: E/AndroidRuntime(819): at android.os.Looper.loop(Looper.java:136)
05-24 02:47:33.640: E/AndroidRuntime(819): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-24 02:47:33.640: E/AndroidRuntime(819): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 02:47:33.640: E/AndroidRuntime(819): at java.lang.reflect.Method.invoke(Method.java:515)
05-24 02:47:33.640: E/AndroidRuntime(819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-24 02:47:33.640: E/AndroidRuntime(819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-24 02:47:33.640: E/AndroidRuntime(819): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
如果你在行
上有NullPointerException for (AlarmModel alarm : alarms) {
然后alarms
必须是null
。您应该在for循环之前进行空检查,或者确保dbHelper.getAlarms()
始终返回非空List,即使它是空的。