2

我正在开发一个提醒应用程序。我在日历中设置提醒。我在 Eclipse 上尝试的代码如下所示:

public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addReminder();
}

public void addReminder(){
    String eventUriString = "content://com.android.calendar/events";
    ContentResolver cr = getContentResolver();
    ContentValues eventValues = new ContentValues();

    eventValues.put(Events.CALENDAR_ID, 1);
    eventValues.put(Events.TITLE, "Hello");
    eventValues.put(Events.DESCRIPTION, "Hello Android");
    eventValues.put(Events.EVENT_TIMEZONE, "India");

    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2013, 01, 01, 18, 05);
    long startMillis = beginTime.getTimeInMillis();

    Calendar endTime = Calendar.getInstance();
    endTime.set(2013, 01, 01, 18, 06);
    long endMillis = endTime.getTimeInMillis();

    eventValues.put(Events.DTSTART, startMillis);
    eventValues.put(Events.DTEND, endMillis);
    eventValues.put("eventStatus", 1);
    eventValues.put("visibility", 3); 
    eventValues.put("transparency", 0); 
    eventValues.put("hasAlarm", 1); 
    Uri eventUri = getContentResolver().insert(Uri.parse(eventUriString), eventValues);
    long eventID = Long.parseLong(eventUri.getLastPathSegment());      

    String reminderUriString = "content://com.android.calendar/reminders";
        ContentValues reminderValues = new ContentValues();

        reminderValues.put("event_id", eventID);
        reminderValues.put("minutes", 5); 
        reminderValues.put("method", 1);
    Uri reminderUri = this.getApplicationContext().getContentResolver().insert(Uri.parse(reminderUriString), reminderValues);
    }
}

我在哪里得到错误:

02-01 17:15:05.855: E/ActivityThread(743): Failed to find provider info for com.android.calendar
02-01 17:15:05.865: D/AndroidRuntime(743): Shutting down VM
02-01 17:15:05.885: W/dalvikvm(743): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-01 17:15:05.905: E/AndroidRuntime(743): FATAL EXCEPTION: main
02-01 17:15:05.905: E/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.testcalendar/com.android.testcalendar.MainActivity}: java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/events
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.os.Looper.loop(Looper.java:123)
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-01 17:15:05.905: E/AndroidRuntime(743):  at java.lang.reflect.Method.invokeNative(Native Method)
02-01 17:15:05.905: E/AndroidRuntime(743):  at java.lang.reflect.Method.invoke(Method.java:507)
02-01 17:15:05.905: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-01 17:15:05.905: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-01 17:15:05.905: E/AndroidRuntime(743):  at dalvik.system.NativeStart.main(Native Method)
02-01 17:15:05.905: E/AndroidRuntime(743): Caused by: java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/events
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.content.ContentResolver.insert(ContentResolver.java:600)
02-01 17:15:05.905: E/AndroidRuntime(743):  at com.ocs.testcalendar.MainActivity.addReminder(MainActivity.java:68)
02-01 17:15:05.905: E/AndroidRuntime(743):  at com.ocs.testcalendar.MainActivity.onCreate(MainActivity.java:19)
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-01 17:15:05.905: E/AndroidRuntime(743):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-01 17:15:05.905: E/AndroidRuntime(743):  ... 11 more

这是我在运行我的应用程序时收到的错误日志。我还提供了日历中的读取和写入权限。

我应该遵循什么步骤或者我应该做什么或改变以避免错误。我在我的模拟器上运行这段代码。

谢谢你。

4

1 回答 1

2

当我在模拟器上运行它时,我的应用程序中会发生此错误。因为在我的模拟器中没有日历可供访问。

于 2013-02-08T11:29:13.903 回答