我在 ICS 中创建新日历时遇到问题。日历应该与谷歌日历同步。我尝试以下:
Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
calendar.put(CalendarContract.Calendars.ACCOUNT_NAME, sync_account);
calendar.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
calendar.put(CalendarContract.Calendars.NAME, name);
calendar.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, displayName);
calendar.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xFF008080);
calendar.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
calendar.put(CalendarContract.Calendars.OWNER_ACCOUNT, true);
calendar.put(CalendarContract.Calendars.VISIBLE, 1);
calendar.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
calendarUri = calendarUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, sync_account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google") // CalendarContract.ACCOUNT_TYPE_LOCAL
.build();
Uri result = activity.getContentResolver().insert(calendarUri, calendar);
我总是得到这个错误(编辑:不再):
09-17 17:11:30.278: E/AndroidRuntime(13243): FATAL EXCEPTION: CalendarSyncAdapterAccountMonitor
09-17 17:11:30.278: E/AndroidRuntime(13243): java.lang.IllegalArgumentException: the name must not be empty: null
09-17 17:11:30.278: E/AndroidRuntime(13243): at android.accounts.Account.<init>(Account.java:48)
09-17 17:11:30.278: E/AndroidRuntime(13243): at com.google.android.syncadapters.calendar.CalendarSyncAdapter.onAccountsUpdated(CalendarSyncAdapter.java:1129)
09-17 17:11:30.278: E/AndroidRuntime(13243): at android.accounts.AccountManager$11.run(AccountManager.java:1279)
09-17 17:11:30.278: E/AndroidRuntime(13243): at android.os.Handler.handleCallback(Handler.java:605)
09-17 17:11:30.278: E/AndroidRuntime(13243): at android.os.Handler.dispatchMessage(Handler.java:92)
09-17 17:11:30.278: E/AndroidRuntime(13243): at android.os.Looper.loop(Looper.java:137)
09-17 17:11:30.278: E/AndroidRuntime(13243): at android.os.HandlerThread.run(HandlerThread.java:60)
09-17 17:11:30.293: E/android.os.Debug(1989): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error
我可以用 CalendarSyncAdapterAccountMonitor 做什么,它不为空?
提前致谢。
编辑
我忘了插入时区,而不是我根本没有收到错误:
calendar.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault().getID());
如果我创建可同步日历(上面的代码片段),只需注意发生,没有错误,也没有创建日历。
如果我将其更改为 lokal(见下文),则可以毫无错误地创建日历,甚至可以添加事件。我该怎么做才能创建可同步的日历?
Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
calendar.put(CalendarContract.Calendars.ACCOUNT_NAME, sync_account);//
//calendar.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
calendar.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
calendar.put(CalendarContract.Calendars.NAME, name);
calendar.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, name);
calendar.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xFF008080);
calendar.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
calendar.put(CalendarContract.Calendars.OWNER_ACCOUNT, sync_account);// true
calendar.put(CalendarContract.Calendars.VISIBLE, 1);
calendar.put(CalendarContract.Calendars.SYNC_EVENTS, 1); // 1 for syncable, 0 for local
//calendar.put(CalendarContract.Calendars.DIRTY, 0);
calendar.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault().getID());
calendarUri = calendarUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, sync_account)//
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL) // "com.google"
.build();
Uri result = activity.getContentResolver().insert(calendarUri, calendar);