通过意图打开Samsung S Planner时出现例外情况

时间:2013-03-07 20:56:38

标签: android android-intent nullpointerexception

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

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

目前唯一的问题是安装了S Planner日历应用的三星设备。执行intent时,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来创建新事件?

2 个答案:

答案 0 :(得分: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 :(得分: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());