当我尝试编辑活动时谷歌日历崩溃了

时间:2012-11-09 11:31:27

标签: android google-calendar-api

我有奇怪的问题,我不知道如何解决这个问题。我测试过很多东西,但不知道是什么问题。

好的我已创建了将事件插入谷歌日历设备的简单程序,这将成功插入谷歌日历。

当我尝试从谷歌日历编辑或点击编辑菜单时,谷歌日历将崩溃。我在许多设备上测试了所有设备日历的问题都是相同的。

这是我的代码

ContentResolver cr = getContentResolver();
        ContentValues values = new ContentValues();
        Uri EVENTS_URI = null;

        EVENTS_URI = Uri.parse("content://com.android.calendar/events");

        long time = System.currentTimeMillis();

        values.put("calendar_id", 1);
        values.put("title", "event.eventName");
        values.put("allDay", 0);
        values.put("dtstart", time); 
        values.put("dtend", time + 1000 * 60 * 60 * 2);
        values.put("description", "description");
        values.put("visibility", 0);
        values.put("transparency", 0);
        values.put("hasAttendeeData", 0);
        values.put("hasAlarm", 1);
        values.put("eventLocation", "location");
        cr.insert(EVENTS_URI, values);

我无法在我的插入事件中检测或说明为什么会发生这种情况

2 个答案:

答案 0 :(得分:0)

如果您考虑使用意图,可以这样做:

Intent intent = new Intent(Intent.ACTION_EDIT);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < ICE_CREAM_BUILD_ID) {
    // all SDK below ice cream sandwich
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", startTime);
    intent.putExtra("endTime", endTime);
    intent.putExtra("title", title);
    intent.putExtra("description", description);
    intent.putExtra("eventLocation", location);
    intent.putExtra("allDay", isAllDay);

//  intent.putExtra("rrule", "FREQ=YEARLY");
} else {
    // ice cream sandwich and above
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime);
    intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime);
    intent.putExtra(Events.TITLE, title);
    intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
    intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , isAllDay);
    intent.putExtra(Events.DESCRIPTION, description);
    intent.putExtra(Events.EVENT_LOCATION, location);

//  intent.putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10") 
}
try {
    context.startActivity(intent);
    return true;
} catch(Exception e) {
    return false;
}

答案 1 :(得分:0)

在分析插​​入事件并在logcat中搜索日历后,我发现当我尝试编辑从我的应用程序中插入的事件时,我的日历强制关闭。

原因是我没有为我的事件设置TimeZone字段值,并且在日历中它将在编辑时获得空指针,这就是强制关闭的原因

我还搜索了android的logcat应用程序我发现了这个

https://play.google.com/store/apps/details?id=org.jtb.alogcat&feature=search_result

从该日志中检测到对我来说非常有用的所有日志我知道TimeZone已填充值null并且日历应用程序已崩溃。

所以我只是在插入时添加这个填充值,它完美地工作。

对于ICS

values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

低于ICS

values.put("eventTimezone", TimeZone.getDefault().getID());