日历编辑事件强制关闭问题

时间:2011-11-23 07:04:40

标签: android

我正在尝试在后台添加日历事件而不使用Intents。事件成功保存。 但是,当我尝试手动编辑该事件时,我会收到强制关闭错误。

int cal_id = getCalendar_ID();
if(cal_id != 0){
 saveCalendarEvent(cal_id);
}
private void saveCalendarEvent(int calid){
    try{
        Calendar cal        = Calendar.getInstance();
        ContentValues event = new ContentValues();
        long startTime      = cal.getTimeInMillis() + 1000 * 60 * 60;
        long endTime        = cal.getTimeInMillis() + 1000 * 60 * 60 * 2;
        event.put("calendar_id", calid);
        event.put("title", "Event Title");
        event.put("description", "Event Desc");
        event.put("eventLocation", "Event Location");           
        event.put("dtstart", startTime);
        event.put("dtend", endTime);
        event.put("allDay", 0);
        Uri newEvent        = getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), event);

        if (newEvent != null) {
            long id = Long.parseLong( newEvent.getLastPathSegment() );
            ContentValues values = new ContentValues();
            values.put( "event_id", id );
            values.put( "method", 1 );
            values.put( "minutes", 15 ); // 15 minuti
            getContentResolver().insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
        }
    }catch(Exception ee){}
}
private int getCalendar_ID() {
    int calendar_id         = 0;
    String[] projection     = new String[] { "_id", "name" };
    String selection        = "selected=1";
    String path             = "calendars";
    Cursor calendarCursor   = getCalendarCursor(projection, selection, path);

    if (calendarCursor != null && calendarCursor.moveToFirst()) {
        int nameColumn      = calendarCursor.getColumnIndex("name");
        int idColumn        = calendarCursor.getColumnIndex("_id");
        do {
            String calName  = calendarCursor.getString(nameColumn);
            String calId    = calendarCursor.getString(idColumn);
            if (calName != null /*&& calName.contains("Test")*/) {
                calendar_id = Integer.parseInt(calId);
            }
        } while (calendarCursor.moveToNext());
    }
    return calendar_id;
}
private Cursor getCalendarCursor(String[] projection, String selection, String path) {
    Uri calendars           = Uri.parse("content://calendar/" + path);
    Cursor cCursor          = null;
    try {
        cCursor             = managedQuery(calendars, projection, selection, null, null);
    } catch (IllegalArgumentException e) {}
    if (cCursor == null) {
        calendars           = Uri.parse("content://com.android.calendar/" + path);
        try {
            cCursor         = managedQuery(calendars, projection, selection, null, null);
        } catch (IllegalArgumentException e) {}
    }
    return cCursor;
}

强制关闭错误日志

11-23 12:22:39.572: E/AndroidRuntime(2630): Caused by: java.lang.NullPointerException
11-23 12:22:39.572: E/AndroidRuntime(2630):     at java.util.TimeZone.getTimeZone(TimeZone.java:286)
11-23 12:22:39.572: E/AndroidRuntime(2630):     at com.android.calendar.TimezoneAdapter.showInitialTimezones(TimezoneAdapter.java:255)
11-23 12:22:39.572: E/AndroidRuntime(2630):     at com.android.calendar.TimezoneAdapter.<init>(TimezoneAdapter.java:198)
11-23 12:22:39.572: E/AndroidRuntime(2630):     at com.android.calendar.EditEvent.onCreate(EditEvent.java:707)
11-23 12:22:39.572: E/AndroidRuntime(2630):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 12:22:39.572: E/AndroidRuntime(2630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1612)
11-23 12:22:39.572: E/AndroidRuntime(2630):     ... 11 more

使用2.3.7设备。感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:1)

您的日历有事件时区字段。所以在你的代码中通过以下行。

event.put(“eventTimezone”,Time.getCurrentTimezone());