2

我正在尝试打开已安装的日历应用程序,以便用户可以输入新事件。我使用以下在 Nexus 和许多其他设备上运行良好的意图。

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中没有找到任何线索。

有人知道如何正确启动 S Planner 来创建新事件吗?

4

2 回答 2

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());
于 2013-03-07T22:12:23.743 回答
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;
        }
    }
}
于 2013-03-07T21:59:59.990 回答