由于内容值中存在IllegalArgumentException,无法插入新日历

时间:2015-08-12 08:16:58

标签: android calendar android-contentprovider android-calendar

我想在CalendarProvider中插入本地日历。这是我的工作:

Uri uri = asSyncAdapter(CalendarContract.Calendars.CONTENT_URI, "Test", CalendarContract.ACCOUNT_TYPE_LOCAL);

        ContentValues values = new ContentValues();

        values.put(CalendarContract.Calendars.NAME, "Test");
        values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "Test Calendar (Added Programmatically)");
        values.put(CalendarContract.Calendars.CALENDAR_COLOR, 0x00FF00);
        values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
        values.put(CalendarContract.Calendars.OWNER_ACCOUNT, "Test");

Uri insertUri = getContentResolver().insert(uri, values);

此方法是从开发人员复制的:

static Uri asSyncAdapter(Uri uri, String account, String accountType) {
    return uri.buildUpon()
            .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType).build();
}

但是我为name列添加了值,我得到了一个类似的例外:

java.lang.IllegalArgumentException: the name must not be empty: null

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我发现了问题。我必须明确为帐户名称和帐户类型设置值:

values.put(CalendarContract.Calendars.ACCOUNT_NAME, "Test");
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);