デバイスの再起動時に自動的にリセットされるアラームを作成しようとしています。これは正しい方法ですか (Android マニフェストが正しいと仮定します)? コードがアプリをクラッシュさせているようです。
public class AlarmReceiver extends BroadcastReceiver {
Context cxt;
@Override
public void onReceive(Context context, Intent intent) {
cxt = context;
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 0);
updateTime.set(Calendar.MINUTE, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
}
}
Logcat は次のとおりです。
01-28 00:35:00.263: E/AndroidRuntime(31269): FATAL EXCEPTION: main
01-28 00:35:00.263: E/AndroidRuntime(31269): java.lang.RuntimeException: Unable to start receiver com.neelsomani.meds.AlarmReceiver: java.lang.NullPointerException
01-28 00:35:00.263: E/AndroidRuntime(31269): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)
01-28 00:35:00.263: E/AndroidRuntime(31269): at android.app.ActivityThread.access$1600(ActivityThread.java:139)
01-28 00:35:00.263: E/AndroidRuntime(31269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
01-28 00:35:00.263: E/AndroidRuntime(31269): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 00:35:00.263: E/AndroidRuntime(31269): at android.os.Looper.loop(Looper.java:137)
01-28 00:35:00.263: E/AndroidRuntime(31269): at android.app.ActivityThread.main(ActivityThread.java:4918)
01-28 00:35:00.263: E/AndroidRuntime(31269): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 00:35:00.263: E/AndroidRuntime(31269): at java.lang.reflect.Method.invoke(Method.java:511)
01-28 00:35:00.263: E/AndroidRuntime(31269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-28 00:35:00.263: E/AndroidRuntime(31269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-28 00:35:00.263: E/AndroidRuntime(31269): at dalvik.system.NativeStart.main(Native Method)
01-28 00:35:00.263: E/AndroidRuntime(31269): Caused by: java.lang.NullPointerException
01-28 00:35:00.263: E/AndroidRuntime(31269): at com.neelsomani.meds.AlarmReceiver.onReceive(AlarmReceiver.java:40)
01-28 00:35:00.263: E/AndroidRuntime(31269): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2258)