Google Calendar API引发的名称不能为空:null

时间:2019-03-01 10:33:38

标签: android google-calendar-api

我正在尝试将事件从我的android应用程序插入到Google日历中,但是该应用程序崩溃了,该名称必须不能为空,以解决此问题,我提供了getaccounts的运行时权限,但问题仍然存在。

这是我的代码: 在oncreate()里面,我做了:

SharedPreferences set = getActivity().getSharedPreferences("PREFS_NAME", 0);
String emailtoken= set.getString("email","").toString();
credential = GoogleAccountCredential.usingOAuth2(
getActivity().getApplicationContext(), Arrays.asList(SCOPES))
        .setSelectedAccountName(emailtoken)
        .setBackOff(new ExponentialBackOff());


mService = new com.google.api.services.calendar.Calendar.Builder(transport, jsonFactory, credential)
        .setApplicationName("Google Calendar API Android Quickstart")
        .build();

这是我插入事件的代码:

Event event = new Event()
        .setSummary(tskname)
        .setDescription(tskdesc);

DateTime startDateTime = new DateTime("2019-05-20T09:00:00-07:00");
EventDateTime start = new EventDateTime()
        .setDateTime(startDateTime)
        .setTimeZone("America/Los_Angeles");
event.setStart(start);

EventAttendee[] attendees = new EventAttendee[] {
        new EventAttendee().setEmail(emailtoken)
};
event.setAttendees(Arrays.asList(attendees));



String calendarId = "primary";
try {
    event = mService.events().insert(calendarId, event).execute();
} catch (IOException e) {
    e.printStackTrace();
}
Log.i("Event created: %s\n", event.getHtmlLink());

这是我的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.pmsapp, PID: 29149
    java.lang.IllegalArgumentException: the name must not be empty: null
        at android.accounts.Account.<init>(Account.java:78)
        at android.accounts.Account.<init>(Account.java:63)
        at com.google.android.gms.auth.zzd.getToken(Unknown Source:1)
        at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source:55)
        at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
        at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
        at com.pmsapp.fragment.EmployeeTask.addtask(EmployeeTask.java:533)
        at com.pmsapp.fragment.EmployeeTask$25.onClick(EmployeeTask.java:392)
        at android.view.View.performClick(View.java:6312)
        at android.view.View$PerformClick.run(View.java:24811)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:171)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

我是使用此api的新手,将不胜感激。

0 个答案:

没有答案