add android calendar events using java

android, android-calendar, java

Solution

Mistake in your code:

- `new String[]{"calendar_id", "displayname"}, null, null, null);`

There is no column exists name calendar_id in Calendar database. Correct is _id not calendar_id. So it will be new String[]{"_id", "displayname"}, null, null, null);

And see this reference. https://stackoverflow.com/a/10310258 And http://android.arnodenhond.com/tutorials/calendar

EDIT

For inserting new event, 3 values are required. Calendar_id, EventStartTime And EventEndTime. Without these values you cannot insert new event. EventStartTime and EventEndTime need into epoch format.

For normal date to epoch date: https://stackoverflow.com/a/6687502/1160207 For epoch to normal date: https://stackoverflow.com/a/10028980/1160207

Here is the code that perfectly working to me.

Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"_id", "displayname"}, null, null, null);

cursor.moveToFirst();
// Get calendars name
String calendarNames[] = new String[cursor.getCount()];
// Get calendars id
int[] calendarId = new int[cursor.getCount()];
for (int i = 0; i < calendarNames.length; i++)
{
         calendarId[i] = cursor.getInt(0);
         calendarNames[i] = cursor.getString(1);
         cursor.moveToNext();
}
cursor.close();

ContentValues contentEvent = new ContentValues();
 contentEvent.put("calendar_id", 1);
 contentEvent.put("title", "Wedding");
 contentEvent.put("eventLocation", "New York");                
 contentEvent.put("dtstart","1335432431000");
 contentEvent.put("dtend","1335436031000");


 Uri eventsUri = Uri.parse("content://com.android.calendar/events");
 getContentResolver().insert(eventsUri, contentEvent);

Hope this will help you. And don't forget to accept it if it is helpful to you.

Thanks...

Problem

Hi I am trying to add event to android calendar. I am new to android development. Please help me how can I add events to android calendar. Here is my code which does not work. When I run it, it gives me a message "The application calendar (process com.something.something) has stopped unexpectedly"and has this button "Force to stop". Here is my code ``` package com.zafar.calendar; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; public class Calendar extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"_id", "displayname"}, null, null, null); cursor.moveToFirst(); // Get calendars name String calendarNames[] = new String[cursor.getCount()]; // Get calendars id int[] calendarId = new int[cursor.getCount()]; for (int i = 0; i < calendarNames.length; i++) { calendarId[i] = cursor.getInt(0); calendarNames[i] = cursor.getString(1); cursor.moveToNext(); } cursor.close(); ContentValues contentEvent = new ContentValues(); contentEvent.put("calendar_id", 1); contentEvent.put("title", "Wedding"); contentEvent.put("eventLocation", "New York"); contentEvent.put("dtstart","1335432431000"); contentEvent.put("dtend","1335436031000"); Uri eventsUri = Uri.parse("content://com.android.calendar/events"); getContentResolver().insert(eventsUri, contentEvent); } } ``` Update Here is my menifest file ``` <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".Calendar" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission> <uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission> </application> ``` Logcat I am new to android development. I do not know logcat exactly. I ran logcat commmand on command prompt and I got this ``` 04-28 09:16:57.956: D/AndroidRuntime(333): Shutting down VM 04-28 09:16:57.956: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560) 04-28 09:16:57.975: E/AndroidRuntime(333): FATAL EXCEPTION: main 04-28 09:16:57.975: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zafar.calendar/com.zafar.calendar.Calendar}: java.lang.NullPointerException 04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 04-28 09:16:57.975: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99) 04-28 09:16:57.975: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123) 04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3683) 04-28 09:16:57.975: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method) 04-28 09:16:57.975: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507) 04-28 09:16:57.975: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 04-28 09:16:57.975: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-28 09:16:57.975: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method) 04-28 09:16:57.975: E/AndroidRuntime(333): Caused by: java.lang.NullPointerException 04-28 09:16:57.975: E/AndroidRuntime(333): at com.zafar.calendar.Calendar.onCreate(Calendar.java:51) 04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 04-28 09:16:57.975: E/AndroidRuntime(333): ... 11 more 04-28 09:21:58.095: I/Process(333): Sending signal. PID: 333 SIG: 9 ```

Original source

Related problems