尝试向日历添加事件时出错:名称不能为空

时间:2017-02-06 17:38:52

标签: android google-calendar-api

我正在尝试使用我的应用在日历上添加新活动。我使用以下代码:

public void createEvent() {
        String token = preferences.getString(Config.GOOGLE_TOKEN, "");
        m_credential = GoogleAccountCredential.usingOAuth2(this.getContext(), SCOPES);
        m_credential.setSelectedAccountName(token);
        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        com.google.api.services.calendar.Calendar service = new com.google.api.services.calendar.Calendar.Builder(
                transport, jsonFactory, m_credential)
                .setApplicationName("R_D_Location Callendar")
                .build();


        Event event = new Event()
                .setSummary("Event- April 2016")
                .setLocation("Dhaka")
                .setDescription("New Event 1");

        DateTime startDateTime = new DateTime("2017-02-26T18:10:00+06:00");
        EventDateTime start = new EventDateTime()
                .setDateTime(startDateTime)
                .setTimeZone("Asia/Dhaka");
        event.setStart(start);

        DateTime endDateTime = new DateTime("2017-02-26T18:40:00+06:00");
        EventDateTime end = new EventDateTime()
                .setDateTime(endDateTime)
                .setTimeZone("Asia/Dhaka");
        event.setEnd(end);

        String[] recurrence = new String[]{"RRULE:FREQ=DAILY;COUNT=2"};
        event.setRecurrence(Arrays.asList(recurrence));

        EventAttendee[] attendees = new EventAttendee[]{
                new EventAttendee().setEmail("abir@aksdj.com"),
                new EventAttendee().setEmail("asdasd@andlk.com"),
        };
        event.setAttendees(Arrays.asList(attendees));

        EventReminder[] reminderOverrides = new EventReminder[]{
                new EventReminder().setMethod("email").setMinutes(24 * 60),
                new EventReminder().setMethod("popup").setMinutes(10),
        };
        Event.Reminders reminders = new Event.Reminders()
                .setUseDefault(false)
                .setOverrides(Arrays.asList(reminderOverrides));
        event.setReminders(reminders);

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

    }

但是在event = service.events().insert(calendarId, event).execute();行中,我发现了以下错误:

E/UncaughtException: java.lang.IllegalArgumentException: the name must not be empty: null
                                                                      at android.accounts.Account.<init>(Account.java:48)
                                                                      at com.google.android.gms.auth.zze.getToken(Unknown Source)
                                                                      at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
                                                                      at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:269)
                                                                      at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:294)
                                                                      at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:868)
                                                                      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.socialcar.booking_fragment.BookingOwnerDetailFragment.createEvent(BookingOwnerDetailFragment.java:456)
                                                                      at com.socialcar.booking_fragment.BookingOwnerDetailFragment.onClick(BookingOwnerDetailFragment.java:299)
                                                                      at android.view.View.performClick(View.java:4615)
                                                                      at android.view.View$PerformClick.run(View.java:18784)
                                                                      at android.os.Handler.handleCallback(Handler.java:733)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:136)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5061)
                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:515)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
                                                                      at dalvik.system.NativeStart.main(Native Method)

我在其他地方检查过以找到解决方案并且错误似乎与令牌有关,但是当我使用谷歌登录时我得到了它。所以我不知道为什么我有这个错误。

1 个答案:

答案 0 :(得分:0)

将您的代码与Calendar Android Quickstart进行比较,如果您在调用events().insert之前确保首先处理了凭据和授权,我认为会更好。

您可以在Android快速入门中看到getResultsFromApi()调用包含

chooseAccount()方法
mCredential.setSelectedAccountName(accountName);

我认为是您的错误所寻找的名称。

private void getResultsFromApi() {
        if (! isGooglePlayServicesAvailable()) {
            acquireGooglePlayServices();
        } else if (mCredential.getSelectedAccountName() == null) {
            chooseAccount();   // <-- makes sure that your account name is verified first
        } else if (! isDeviceOnline()) {
            mOutputText.setText("No network connection available.");
        } else {
            new MakeRequestTask(mCredential).execute(); // <-- after authentication and authorization, you can now make a request to Calendar API
        }
    }