2016-06-23 12 views
-1

私のコードに少し問題があります。私はAndroidスタジオを使用しています。 私は時間を表示する携帯電話のカレンダーのイベントにアプリケーションを作ろうとしています。私はすべてのエラーを持っていないが、私は自分の携帯電話にそれを実行しようとしたときに起因アンドロイドカレンダーのイベントを読む

:ここ

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


} 

主な活動:

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

答えて

0

まず、uriアドレスを確認してください。たとえば 'content:// calendar/calendar'のように、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 
}; 

第3に、コードの方法が非常に悪いです。これらのパラメータを「静的最終」として宣言する必要があります。