Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android日历读取事件_Android_Android Studio_Calendar_Android Calendar - Fatal编程技术网

从android日历读取事件

从android日历读取事件,android,android-studio,calendar,android-calendar,Android,Android Studio,Calendar,Android Calendar,我的代码有点问题。我使用Android Studio。 我尝试在手机的日历事件中创建一个显示时间的应用程序。我没有任何错误,但当我试图运行到我的手机 原因: java.lang.NullPointerException at net.jimblackler.readcalendar.Example.readCalendar(Example.java:29)

我的代码有点问题。我使用Android Studio。 我尝试在手机的日历事件中创建一个显示时间的应用程序。我没有任何错误,但当我试图运行到我的手机

原因:

java.lang.NullPointerException
at net.jimblackler.readcalendar.Example.readCalendar(Example.java:29)
                                                                                  at net.jimblackler.readcalendar.MainActivity.onCreate(MainActivity.java:14)
这是我的cod:

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);
            }
        }
    }


}

首先,请检查uri地址。例如'content://calendar/calendars,它没有在安卓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  
};  
第三,您的编码方式非常糟糕。您应该将上述参数声明为“静态最终”参数

/** 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  
};