我正在尝试打开已安装的日历应用程序,以便用户可以输入新事件。我使用以下意图在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
中发生的事情时,我没有在publicly available sources中找到任何线索。有人知道如何正确启动S Planner来创建新事件吗?
最佳答案
我已经创建了用于创建日历事件的实用程序类,希望您发现它有用:
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;
}
}
}
关于android - 通过 Intent 打开Samsung S Planner时发生异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15281573/