2013-03-07 52 views
2

我正在嘗試打開已安裝的日曆應用程序,以便用戶可以輸入新事件。我使用以下在Nexus和許多其他設備上工作正常的意圖。通過意圖打開Samsung S Planner時出現異常

Intent intent = new Intent(Intent.ACTION_EDIT); 
intent.setType("vnd.android.cursor.item/event"); 

到目前爲止唯一的問題是安裝了S Planner日曆應用程序的三星設備。當意圖執行時,LogCat中顯示以下異常,並且S Planner應用程序崩潰(使用系統錯誤對話框)。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.calendar/com.android.calendar.event.EditEventActivity}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
    at android.app.ActivityThread.access$700(ActivityThread.java:140) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4921) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
    at com.android.calendar.CalendarEventModel.(CalendarEventModel.java:406) 
    at com.android.calendar.event.EditEventFragment.onAttach(EditEventFragment.java:548) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:787) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) 
    at android.app.BackStackRecord.run(BackStackRecord.java:635) 
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399) 
    at android.app.Activity.performStart(Activity.java:5215) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083) 
    ... 11 more 

雖然試圖追查是怎麼回事,在CalendarEventModel我沒有找到在publicly available sources任何線索它。

有沒有人有一個想法如何正確啓動S Planner來創建一個新的事件?

+0

[日曆提供引導(http://developer.android.com/guide/topics/providers/calendar-provider.html)具有[添加新的事件](HTTP一些示例://顯影劑。 android.com/guide/topics/providers/calendar-provider.html#add-event),[更新活動](http://developer.android.com/guide/topics/providers/calendar-provider.html#update-事件),[刪除事件](http://developer.android.com/guide/topics/providers/calendar-provider.html#delete-event)...我希望它會有用。 – 2013-03-07 21:45:51

+0

感謝您的提示。我幫助走上了正確的軌道。 – Moritz 2013-03-07 22:13:02

回答

1

我創建了這個工具類,用於創建日曆事件,希望你覺得它有用:

public class CalendarOrganizer { 
    private final static int ICE_CREAM_BUILD_ID = 14; 
    /** 
    * Creates a calendar intent going from startTime to endTime 
    * @param startTime 
    * @param endTime 
    * @param context 
    * @return true if the intent can be handled and was started, 
    * false if the intent can't be handled 
    */ 
    public static boolean createEvent(long startTime, long endTime, String title, String description, 
      String location, boolean isAllDay, Context context) { 
     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

感謝您的各種答覆。我已經知道了。 S Planner需要設置開始日期和結束日期。否則它崩潰。

 
Intent intent = new Intent(Intent.ACTION_INSERT).setData(Events.CONTENT_URI) 
    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getMillis()) 
    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getMillis()); 
相關問題