面对Android中的日历应用程序问题

时间:2012-12-02 19:33:21

标签: android android-intent android-activity calendar

我正在尝试创建一个Intent来创建Calendar活动。以下是代码:

public void onClickCalendarEntry(View view){
        Intent calendarIntent = new Intent(Intent.ACTION_INSERT);
        calendarIntent.setType("com.android.calendar/events");
        calendarIntent.putExtra(Events.TITLE, "Learn Android");
        calendarIntent.putExtra(Events.EVENT_LOCATION, "Home");
        calendarIntent.putExtra(Events.DESCRIPTION, "Learn activities");

        //Setting dates
        GregorianCalendar calDate = new GregorianCalendar(2012,12,2);
        calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
                calDate.getTimeInMillis());
        calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
                calDate.getTimeInMillis());


        Log.d(tag, "Before startActivity()");
        startActivity(calendarIntent);
        Log.d(tag, "Afte startActivity()");
    }

现在下面是AndroidManifest.exml摘录:

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

以下是我收到的错误消息:

12-02 20:27:54.958: D/MainActivity(5512): Before startActivity()
12-02 20:27:54.968: D/AndroidRuntime(5512): Shutting down VM
12-02 20:27:54.978: W/dalvikvm(5512): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
12-02 20:27:54.978: E/AndroidRuntime(5512): FATAL EXCEPTION: main
12-02 20:27:54.978: E/AndroidRuntime(5512): java.lang.IllegalStateException: Could not execute method of the activity
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.view.View$1.onClick(View.java:2168)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.view.View.performClick(View.java:2552)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.view.View$PerformClick.run(View.java:9229)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.os.Handler.handleCallback(Handler.java:587)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.os.Looper.loop(Looper.java:130)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.app.ActivityThread.main(ActivityThread.java:3701)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at java.lang.reflect.Method.invokeNative(Native Method)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at java.lang.reflect.Method.invoke(Method.java:507)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at dalvik.system.NativeStart.main(Native Method)
12-02 20:27:54.978: E/AndroidRuntime(5512): Caused by: java.lang.reflect.InvocationTargetException
12-02 20:27:54.978: E/AndroidRuntime(5512):     at java.lang.reflect.Method.invokeNative(Native Method)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at java.lang.reflect.Method.invoke(Method.java:507)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.view.View$1.onClick(View.java:2163)
12-02 20:27:54.978: E/AndroidRuntime(5512):     ... 11 more
12-02 20:27:54.978: E/AndroidRuntime(5512): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT typ=com.android.calendar/events (has extras) }
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.app.Activity.startActivityForResult(Activity.java:2827)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at android.app.Activity.startActivity(Activity.java:2933)
12-02 20:27:54.978: E/AndroidRuntime(5512):     at com.example.sendingusertoanotherapp.MainActivity.onClickCalendarEntry(MainActivity.java:88)
12-02 20:27:54.978: E/AndroidRuntime(5512):     ... 14 more

任何人都可以帮我理解这里的问题是什么?感谢。

关于, Sudipta Deb。

2 个答案:

答案 0 :(得分:2)

弗洛约之前的{p> "calendar/events"

"com.android.calendar/events"后Froyo。

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
    calendarIntent.setType("calendar/events");
} else {
    calendarIntent.setType("com.android.calendar/events");
}

答案 1 :(得分:0)

最后问题解决了:

以下是我现在使用的代码:

Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", false);
intent.putExtra("rrule", "FREQ=DAILY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);

感谢所有人的帮助。

关于, Sudipta Deb