将值插入Google日历

时间:2013-01-16 04:54:14

标签: android google-calendar-api

我在运行此代码时遇到错误... 这是我的代码:

alendar cal = Calendar.getInstance();     
    Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
    ContentResolver cr = getContentResolver();

    // event insert
    ContentValues values = new ContentValues();
    values.put("calendar_id", 1);
    values.put("title", edSubject.getText().toString());
    values.put("allDay", 0);
    values.put("dtstart", dateStart.toString()); // event starts at 11 minutes from now
    values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
    values.put("description", "Reminder description");
    values.put("visibility", 0);
    values.put("hasAlarm", 1);
    Uri event = cr.insert(EVENTS_URI, values);

    // reminder insert
    /*Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
    values = new ContentValues();
    values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
    values.put( "method", 1 );
    values.put( "minutes", 10 );
    cr.insert( REMINDERS_URI, values );*/

 private String getCalendarUriBase(ActivityAppointment act) {

        String calendarUriBase = null;
        Uri calendars = Uri.parse("content://calendar/calendars");
        Cursor managedCursor = null;
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://calendar/";
        } else {
            calendars = Uri.parse("content://com.android.calendar/calendars");
            try {
                managedCursor = act.managedQuery(calendars, null, null, null, null);
            } catch (Exception e) {
            }
            if (managedCursor != null) {
                calendarUriBase = "content://com.android.calendar/";
            }
        }
        return calendarUriBase;
    }

logcat:

01-16 15:34:27.620: E/AndroidRuntime(3114): FATAL EXCEPTION: main
01-16 15:34:27.620: E/AndroidRuntime(3114): java.lang.NullPointerException
01-16 15:34:27.620: E/AndroidRuntime(3114):     at android.os.Parcel.readException(Parcel.java:1327)
01-16 15:34:27.620: E/AndroidRuntime(3114):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
01-16 15:34:27.620: E/AndroidRuntime(3114):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
01-16 15:34:27.620: E/AndroidRuntime(3114):     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:447)
01-16 15:34:27.620: E/AndroidRuntime(3114):     at android.content.ContentResolver.insert(ContentResolver.java:721)
01-16 15:34:27.620: E/AndroidRuntime(3114):     at com.sree.weekdayview.calendar.activities.ActivityAppointment.SaveData(ActivityAppointment.java:348)
01-16 15:34:27.620: E/AndroidRuntime(3114):     at com.sree.weekdayview.calendar.activities.ActivityAppointment$3.onClick(ActivityAppointment.java:117)
01-16 15:34:27.620: E/AndroidRuntime(3114):     at android.view.View.performClick(View.java:3122)

1 个答案:

答案 0 :(得分:0)

而不是使用managedQuery尝试内容解析器查询方法。它与managedQuery完全相同,只需将其替换为查询,如下所示。

ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(intent.getData(), null, null, null, null);