引导接收器导致应用程序停止

时间:2012-07-15 13:10:50

标签: java android boot forceclose

我的作业计划应用程序中有一个启动接收器,用于重新设置所有创建的警报,以提醒用户他们的作业到期。每次我的手机启动,我得到"家庭作业计划器已停止& #34;,我无法弄清楚为什么。我不能使用LogCat,因为它没有在启动时连接(?)。

这是我在启动接收器中的代码。

public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // RE-SET ALL ALARMS FROM THE DATABASE WHEN PHONE IS TURNED ON

    HomeworkDatabase db = new HomeworkDatabase(context);
    db.open();
    ArrayList<HomeworkItem> hwks = db.getHomeworks();
    db.close();

    int reminders = 0;

    for (int e = 0; e < hwks.size(); e++) {

        HomeworkItem current = hwks.get(e);

        {
            Calendar c = Calendar.getInstance();
            c.set(current.year, current.month, current.day, 0, 0);

            long alarmTime = c.getTimeInMillis();

            Intent i = new Intent(HomeworkList.getAppContext(),
                    RegularAlarm.class);
            i.putExtra("title", current.title);
            i.putExtra("id", current.id);
            PendingIntent pi = PendingIntent.getBroadcast(
                    HomeworkList.getAppContext(), (int) current.id, i, 0);
            AlarmManager mAlarm = (AlarmManager) context
                    .getApplicationContext().getSystemService(
                            Context.ALARM_SERVICE);
            mAlarm.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);

            reminders++;
        }

        if (current.oneDayReminder == true) {

            Calendar c = Calendar.getInstance();
            c.set(current.year, current.month, current.day, 0, 0);
            c.roll(Calendar.DAY_OF_MONTH, -1);

            long alarmTime = c.getTimeInMillis();

            Intent i = new Intent(HomeworkList.getAppContext(),
                    OneDayAlarm.class);
            i.putExtra("title", current.title);
            i.putExtra("id", current.id);
            PendingIntent pi = PendingIntent.getBroadcast(
                    HomeworkList.getAppContext(), (int) current.id, i, 0);
            AlarmManager mAlarm = (AlarmManager) context
                    .getApplicationContext().getSystemService(
                            Context.ALARM_SERVICE);
            mAlarm.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);

            reminders++;

        }
        if (current.twoDayReminder == true) {
            Calendar c = Calendar.getInstance();
            c.set(current.year, current.month, current.day, 0, 0);
            c.roll(Calendar.DAY_OF_MONTH, -2);

            long alarmTime = c.getTimeInMillis();

            Intent i = new Intent(HomeworkList.getAppContext(),
                    TwoDaysAlarm.class);
            i.putExtra("title", current.title);
            i.putExtra("id", current.id);
            PendingIntent pi = PendingIntent.getBroadcast(
                    HomeworkList.getAppContext(), (int) current.id, i, 0);
            AlarmManager mAlarm = (AlarmManager) context
                    .getApplicationContext().getSystemService(
                            Context.ALARM_SERVICE);
            mAlarm.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);

            reminders++;
        }
    }

    Log.d("hwk","BOOT COMPLETE: " + reminders + " alarms created");
    Toast.makeText(context, reminders + " alarms created at boot", Toast.LENGTH_LONG);

}
}

对于长代码段,我很抱歉,但我不知道问题是什么。

由于

编辑:错误的来源是在创建意图并从PendingIntent获取上下文时。

Intent i = new Intent(HomeworkList.getAppContext(), RegularAlarm.class);

此行导致空指针异常。知道为什么吗?

0 个答案:

没有答案