I want to add a new calendar to Android that does not get synchronized. I have written an App that does this using the new CalendarContract API from API level 14. The following code works fine. I can see the new calendar in Android's calendar app and add events to it.
The problem is that the Android calendar app crashes when I select: Menu -> Calendars to display -> Calendars to sync
Is there something wrong in my code or is it a bug in Android calendar?
Mind that I do not want to actually use a sync adapter. The documentation of CalendarContract.Calendars says that this is not needed: "If a local calendar is required an app can do so by inserting as a sync adapter and using an ACCOUNT_TYPE of ACCOUNT_TYPE_LOCAL." I understand that "inserting as a sync adapter" means to append (CalendarContract.CALLER_IS_SYNCADAPTER, "true") to the URI.
public class CalendarMapper {
private static final String ACCOUNT_NAME = "private";
private static final String INT_NAME_PREFIX = "priv";
private static Uri buildCalUri() {
return CalendarContract.Calendars.CONTENT_URI
.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, ACCOUNT_NAME)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
}
private static ContentValues buildContentValues(Calendar calendar) {
String dispName = calendar.getName(); //Calendar.getName() returns a String
String intName = INT_NAME_PREFIX + dispName;
final ContentValues cv = new ContentValues();
cv.put(Calendars.ACCOUNT_NAME, ACCOUNT_NAME);
cv.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
cv.put(Calendars.NAME, intName);
cv.put(Calendars.CALENDAR_DISPLAY_NAME, dispName);
cv.put(Calendars.CALENDAR_COLOR, calendar.getColor()); //Calendar.getColor() returns int
cv.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
cv.put(Calendars.OWNER_ACCOUNT, ACCOUNT_NAME);
cv.put(Calendars.VISIBLE, 1);
cv.put(Calendars.SYNC_EVENTS, 1);
return cv;
}
public static void addCalendar(Calendar calendar, ContentResolver cr) {
if (calendar == null)
throw new IllegalArgumentException();
final ContentValues cv = buildContentValues(calendar);
Uri calUri = buildCalUri();
cr.insert(calUri, cv);
}
}
Here is the Stack Trace of the Exception that causes the calendar app to crash:
03-22 07:12:39.437: E/AndroidRuntime(9730): FATAL EXCEPTION: main
03-22 07:12:39.437: E/AndroidRuntime(9730): java.lang.NullPointerException
03-22 07:12:39.437: E/AndroidRuntime(9730): at com.android.calendar.selectcalendars.SelectSyncedCalendarsMultiAccountAdapter.bindGroupView(SelectSyncedCalendarsMultiAccountAdapter.java:324)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.CursorTreeAdapter.getGroupView(CursorTreeAdapter.java:207)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ListView.makeAndAddView(ListView.java:1772)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ListView.fillDown(ListView.java:672)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ListView.fillFromTop(ListView.java:732)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ListView.layoutChildren(ListView.java:1611)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.os.Looper.loop(Looper.java:137)
03-22 07:12:39.437: E/AndroidRuntime(9730): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-22 07:12:39.437: E/AndroidRuntime(9730): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 07:12:39.437: E/AndroidRuntime(9730): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 07:12:39.437: E/AndroidRuntime(9730): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-22 07:12:39.437: E/AndroidRuntime(9730): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-22 07:12:39.437: E/AndroidRuntime(9730): at dalvik.system.NativeStart.main(Native Method)
This is an Android bug and apparently will be fixed: http://code.google.com/p/android/issues/detail?id=27474
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With