I am going to create the recreat the alarm while Android OS bootup.
that why i am using this code in manifest:
<receiver android:name=".RecreateTwoMonthAlarm" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Well, it runs perfect. but every time while i bootup my device, i got NullPointer Exception.
Here is my code onReceive method:
//I am going to inatialize myPrefs here
myPrefs = context.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
// but i got symtax error like: MODE_WORLD_READABLE cannot be resolved to a variable
Calendar calendar_GST_18_June_2011 = Calendar.getInstance();
calendar_GST_18_June_2011.setTimeInMillis(System.currentTimeMillis());
calendar_GST_18_June_2011.set(2011, 5, 18, myPrefs.getInt("hour", 00), myPrefs.getInt("minute", 00), 0); // here i got exception
if(!(calendar_GST_18_June_2011.getTimeInMillis()<=currentTime)){
AM_2M_GST_1 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
intent = new Intent(context, AlarmReceiverNotificationForTwoMonth.class);
intent.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 20th June 2011.");
PI_2M_GST_1 = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AM_2M_GST_1.set(AlarmManager.RTC_WAKEUP, calendar_GST_18_June_2011.getTimeInMillis(), PI_2M_GST_1);
}
// for the GST 19 August 2011
Calendar calendar_GST_17_August_2011 = Calendar.getInstance();
calendar_GST_17_August_2011.setTimeInMillis(System.currentTimeMillis());
calendar_GST_17_August_2011.set(2011, 7, 17,myPrefs.getInt("hour", 00), myPrefs.getInt("minute", 00), 0);
if(!(calendar_GST_17_August_2011.getTimeInMillis()<=currentTime)){
AM_2M_GST_2 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent in2 = new Intent(context, AlarmReceiverNotificationForTwoMonth.class);
in2.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 19th August 2011.");
PI_2M_GST_2 = PendingIntent.getBroadcast(context, 1, in2, PendingIntent.FLAG_UPDATE_CURRENT);
AM_2M_GST_2.set(AlarmManager.RTC_WAKEUP, calendar_GST_17_August_2011.getTimeInMillis(),PI_2M_GST_2);
}
i think it is because i am using the shared preference data to set alarm at specific time. Isen’t it ??
am i not able to set the Shared Preference Data in to Bootup of Device or why i got that exception ??
Error Log:
02-03 12:19:27.612: ERROR/AndroidRuntime(246): FATAL EXCEPTION: main
02-03 12:19:27.612: ERROR/AndroidRuntime(246): java.lang.RuntimeException: Unable to start receiver com.project.TaxToolbox.RecreateTwoMonthAlarm: java.lang.NullPointerException
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.os.Looper.loop(Looper.java:123)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at java.lang.reflect.Method.invoke(Method.java:521)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at dalvik.system.NativeStart.main(Native Method)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): Caused by: java.lang.NullPointerException
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at com.project.TaxToolbox.RecreateTwoMonthAlarm.onReceive(RecreateTwoMonthAlarm.java:46)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): ... 10 more
Please help me for that.
Thanks.
Try using Context.WORLD_MODE_READABLE, depending on where you are calling this piece of code, it might not think that “this” is type Context (for example inside an anonymous function or inner class)