getCalendarById(1), null pointer exception

You must Login before you can answer or comment on any questions.

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??!!

— asked 2 years ago by Haya aziz
2 Comments
  • sometimes it works well for the next time, but surely it is not after opening the calendar and then reopen the application

    — commented 2 years ago by Haya aziz

  • Please .. I need a help It seems like can't access the calendar twice !! if I kill the app using 'advanced task killer' then reopen it, it works well !

    is there anyway to kill the app by the code , so it will not work in the back ground

    — commented 2 years ago by Haya aziz

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.

— answered 2 years ago by Emrah Mehmedov
answer permalink
1 Comment
  • Thanks for your effort, I was trying "exitOnClose" and Activity.finish(), but both didn't solve the problem

    — commented 2 years ago by Haya aziz

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();

— answered 1 year ago by han xiangdong
answer permalink
5 Comments
  • Ti.Android.Calendar returns empty array (0), i don't understan why? The cycle is not going coz Ti.Android.Calendar's length is 0???

    — commented 1 year ago by Umidjon Umidjon

  • hi I have followed the above code,and its working fine.But i also want to add the event Location. Please help..

    — commented 12 months ago by Richard JOSEPH

  • You can use location attribute

    var details = {             
        title : 'Calendar Event',
        description : 'Event Description',
        begin : new Date(2012, 5, 24, 11, 20, 0),
        location : 'India',
    };
     
    var event = currentWindow.calendarReference.createEvent(details);

    — commented 12 months ago by Mayank Sharma

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!