-2
基本的にFirebaseAuth経由でGoogleカレンダーからデータを取得するためのアクティビティを作成しようとしています。 FirebaseUserオブジェクトを適切なアカウントオブジェクトに変換して、Googleカレンダーからイベントメソッドを正しく呼び出すことができました。しかし、イベントオブジェクトとき、私は問題に直面してることはnullになり、ここに私のコードは次のとおりです。AndroidのCalendarProviderからNull例外を処理する
// Breakthrough 1
// Miguel has created a wonderful Google Cal API Kickstarter Repo to refer to.
// https://github.com/miguelarauj1o/GoogleCalendarQuickStart
/**
* Breakthrough 2
*
* Several issues such as Firebase Support was fixed by attempting to convert FirebaseUser
* data into an Android.Account Object so that CalendarProvider is able to identify the user
* properly.
*
* However, issues regarding threading came up with an IllegalArgumentException from the start
* and lots of research was done and these are the helpers to the solution.
* http://stackoverflow.com/questions/20749044/how-to-get-rid-of-java-lang-illegalstateexception-while-trying-to-getauthtoken-f
* http://stackoverflow.com/questions/17547019/calling-this-from-your-main-thread-can-lead-to-deadlock-and-or-anrs-while-getti
*/
public class CalendarProvider extends AsyncTask<Void, Void, Events> {
static final HttpTransport transport = AndroidHttp.newCompatibleTransport();
static final JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
static final int REQUEST_ACCOUNT_PICKER = 1000;
static final int REQUEST_AUTHORIZATION = 1001;
static final int REQUEST_GOOGLE_PLAY_SERVICES = 1002;
private static Events events;
private static DateTime dt;
private static final String PREF_ACCOUNT_NAME = "accountName";
private static final String[] SCOPES = { CalendarScopes.CALENDAR_READONLY };
public synchronized static void InitializeCal(SharedPreferences sp, GoogleAccountCredential credential, FirebaseUser user, Context AppContext) {
credential = GoogleAccountCredential.usingOAuth2(
AppContext, Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(sp.getString(PREF_ACCOUNT_NAME, user.getEmail()));
mService = new com.google.api.services.calendar.Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName("Google Calendar API Android Quickstart")
.build();
}
/**
* Fetch a list of the next 10 events from the primary calendar.
* @return List of Strings describing returned events.
* @throws IOException
*/
public static List<String> getDataFromApi() throws IOException {
// List the next 10 events from the primary calendar.
List<String> eventStrings = new ArrayList<>();
try {
if (events.getItems() != null) { // If there are events on this date
List<Event> items = events.getItems();
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
// All-day events don't have start times, so just use
// the start date.
start = event.getStart().getDate();
}
eventStrings.add(
String.format("%s (%s)", event.getSummary(), start));
}
}
} catch (Exception e) {
// Else we add a string to tell'em that nothing is here today
eventStrings.add("Nothing Today!");
// Debugging String
eventStrings.add(e.toString());
}
return eventStrings;
}
@Override
protected Events doInBackground(Void... params) {
dt = new DateTime(selectedDate);
try {
events = mService.events().list("primary")
.setMaxResults(10)
.setTimeMin(dt)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
return events;
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Events events) {
try {
System.out.println(getDataFromApi());
} catch (IOException e) {
e.printStackTrace();
}
}
}
私はどのように適切なNullPointerExceptionsがを処理するために思っていたエラーが意図的かなかった場合、私は見当がつかないので。
もしそれだったら、私にとっては簡単です。ユーザーが選択した特定の日付は何もないと仮定します。
しかし、そうでない場合はどうなりますか?
ありがとうございました! – Nicholas