2011-11-23 88 views
0

我想在不使用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

你的日曆有事件時區字段。所以請在代碼中通過下面的行。

event.put(「eventTimezone」,Time.getCurrentTimezone());