从Android日历中读取事件

时间:2016-06-23 14:20:11

标签: android android-studio calendar android-calendar

我的代码有点问题。我使用Android Studio。 我尝试在手机的日历活动中显示时间。我没有任何错误,但当我尝试将其运行到手机时

引起:

java.lang.NullPointerException
at net.jimblackler.readcalendar.Example.readCalendar(Example.java:29)
                                                                                  at net.jimblackler.readcalendar.MainActivity.onCreate(MainActivity.java:14)

这是我的鳕鱼:

Java类:

import java.util.Date;
import java.util.HashSet;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.format.DateUtils;

public class Example {

    public static void readCalendar(Context context) {

        ContentResolver contentResolver = context.getContentResolver();

        // Fetch a list of all calendars synced with the device, their display names and whether the
        // user has them selected for display.

        final Cursor cursor = contentResolver.query(Uri.parse("content://calendar/calendars"),
                (new String[] { "_id", "displayName", "selected" }), null, null, null);


        HashSet<String> calendarIds = new HashSet<String>();


       while (cursor.moveToNext()) {

            final String _id = cursor.getString(0);
            final String displayName = cursor.getString(1);
            final Boolean selected = !cursor.getString(2).equals("0");

            System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
            calendarIds.add(_id);
        }

        // For each calendar, display all the events from the previous week to the end of next week.
        for (String id : calendarIds) {
            Uri.Builder builder = Uri.parse("content://calendar/instances/when").buildUpon();
            long now = new Date().getTime();
            ContentUris.appendId(builder, now - DateUtils.WEEK_IN_MILLIS);
            ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS);

            Cursor eventCursor = contentResolver.query(builder.build(),
                    new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
                    null, "startDay ASC, startMinute ASC");


            while (eventCursor.moveToNext()) {
                final String title = eventCursor.getString(0);
                final Date begin = new Date(eventCursor.getLong(1));
                final Date end = new Date(eventCursor.getLong(2));
                final Boolean allDay = !eventCursor.getString(3).equals("0");

                System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
                        " All Day: " + allDay);
            }
        }
    }


}

MainActivity:

package net.jimblackler.readcalendar;

import android.app.Activity;
import android.os.Bundle;



public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Example.readCalendar(this);
    }
}

1 个答案:

答案 0 :(得分:0)

首先,请查看uri地址。例如&#39; content:// calendar / calendars&#39; ,它没有在Android 2.1上使用。  在Android 14之前:

calanderURL = "content://calendar/calendars";  
calanderEventURL = "content://calendar/events";  
calanderRemiderURL= "content://calendar/reminders";  

后:

calanderURL = "content://com.android.calendar/calendars";  
calanderEventURL = "content://com.android.calendar/events";  
calanderRemiderURL = "content://com.android.calendar/reminders"; 

但是,您最好这样使用:

private Uri calendarsUri = Calendars.CONTENT_URI;  
private Uri eventsUri = Events.CONTENT_URI;  
private Uri remindersUri = Reminders.CONTENT_URI;  
private Uri attendeesUri = Attendees.CONTENT_URI; 

其次,请检查表格列名称。您可以打印以下列并查看。

/** Calendars table columns */  
public static final String[] CALENDARS_COLUMNS = new String[] {  
    Calendars._ID,                           // 0  
    Calendars.ACCOUNT_NAME,                  // 1  
    Calendars.CALENDAR_DISPLAY_NAME,         // 2  
    Calendars.OWNER_ACCOUNT                  // 3  
};  

/** Events table columns */  
public static final String[] EVENTS_COLUMNS = new String[] {  
    Events._ID,  
    Events.CALENDAR_ID,  
    Events.TITLE,  
    Events.DESCRIPTION,  
    Events.EVENT_LOCATION,  
    Events.DTSTART,  
    Events.DTEND,  
    Events.EVENT_TIMEZONE,            
    Events.HAS_ALARM,  
    Events.ALL_DAY,  
    Events.AVAILABILITY,  
    Events.ACCESS_LEVEL,  
    Events.STATUS,  
};  
/** Reminders table columns */  
public static final String[] REMINDERS_COLUMNS = new String[] {  
    Reminders._ID,  
    Reminders.EVENT_ID,  
    Reminders.MINUTES,  
    Reminders.METHOD,  
};  
/** Reminders table columns */  
public static final String[] ATTENDEES_COLUMNS = new String[] {  
    Attendees._ID,  
    Attendees.ATTENDEE_NAME,  
    Attendees.ATTENDEE_EMAIL,  
    Attendees.ATTENDEE_STATUS  
};  

第三,你的代码方式非常糟糕。你应该将上述参数声明为&#39; static final&#39;那些 。