Dear all .. I'm trying to add events to Android calendar using the code in the API
var CALENDAR_TO_USE = 1; var calendar = Ti.Android.Calendar.getCalendarById(CALENDAR_TO_USE); // Create the event var eventBegins = new Date(2010, 11, 26, 12, 0, 0); var eventEnds = new Date(2010, 11, 26, 14, 0, 0); var details = { title: 'Do some stuff', description: "I'm going to do some stuff at this time.", begin: eventBegins, end: eventEnds }; var event = calendar.createEvent(details); // Now add a reminder via e-mail for 10 minutes before the event. var reminderDetails = { minutes: 10, method: Ti.Android.Calendar.METHOD_EMAIL }; event.createReminder(reminderDetails);the calendar ID is 1 , which is the main native android calendar, everything goes will when opening the application for the first time on my HTC hero device
if I close the app. and then reopen it again .. an error appear in
var calendar = Ti.Android.Calendar.getCalendarById(CALENDAR_TO_USE);which is "NullPointerException"
I don't know why, it works for first time opening but it doesn't for the second time??!!
3 Answers
Accepted Answer
I faced the same problem and after a lot of experiments finally able to fix this problem. Declare the calendar object in app.js file only.
var CALENDAR_TO_USE = 1; var calendar = Ti.Android.Calendar.getCalendarById(CALENDAR_TO_USE);Now pass this calendar object to the other .js file in which you are creating event like
var win2 = Titanium.UI.createWindow({ url:'win2.js', title:'New Window', calendarReference:calendar }); win2.open({modal:true});Now use this reference to save events in win2.js file.
var currentWindow = Ti.UI.currentWindow; var event = currentWindow.calendarReference.createEvent(details);Now it's working fine for me. Hope this helps you.
Use exitOnClose to kill the application when you use android back button. i will try some workarounds when i go to my home.
The same code?but I do not know why my code doesnt work.
Titanium.UI.setBackgroundColor('#3333');
var win = Ti.UI.createWindow({ layout:'vertical', backgroundColor:"red" });
var CALENDAR_TO_USE = 1; var calendar = Ti.Android.Calendar.getCalendarById(CALENDAR_TO_USE);
var eventBegins = new Date(2011, 12, 31, 17, 20, 0); var eventEnds = new Date(2011, 12, 31, 17, 21, 0); var details = { title: 'Do some stuff', description: "I'm going to do some stuff at this time.", begin: eventBegins, end: eventEnds };
var event = calendar.createEvent(details);
// Now add a reminder via e-mail for 1 minutes before the event. var reminderDetails = { minutes: 1, method: Ti.Android.Calendar.METHOD_EMAIL }; event.createReminder(reminderDetails);
win.open();
Your Answer
Think you can help? Login to answer this question!