首页 > 解决方案 > DriveServiceHelper 无法转换为 Parcelable

问题描述

我有一个 android 应用程序,它在开始时进行登录过程,以便允许用户将文件保存在 google 驱动器文件夹中。现在,我想通过活动传递 DriveServiceHelper 对象,因此我不需要再次进行登录过程,但我收到错误:

java.lang.ClassCastException: DriveServiceHelper 无法转换为 android.os.Parcelable 我该如何解决?这是我的登录过程:

private void requestSignIn() {
    Log.d(TAG, "Requesting sign-in");
    GoogleSignInOptions signInOptions =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
                    .build();
    GoogleSignInClient client = GoogleSignIn.getClient(this, signInOptions);
    // The result of the sign-in Intent is handled in onActivityResult.
    startActivityForResult(client.getSignInIntent(), REQUEST_CODE_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    switch (requestCode) {
        case REQUEST_CODE_SIGN_IN:
            if (resultCode == this.RESULT_OK && resultData != null) {
                handleSignInResult(resultData);
            }
            break;
    }

    super.onActivityResult(requestCode, resultCode, resultData);
}

private void handleSignInResult(Intent result) {
    GoogleSignIn.getSignedInAccountFromIntent(result)
            .addOnSuccessListener(googleAccount -> {
                Log.d(TAG, "Signed in as " + googleAccount.getEmail());
                // Use the authenticated account to sign in to the Drive service.
                GoogleAccountCredential credential =
                        GoogleAccountCredential.usingOAuth2(
                                this, Collections.singleton(DriveScopes.DRIVE_FILE));
                credential.setSelectedAccount(googleAccount.getAccount());
                Drive googleDriveService =
                        new Drive.Builder(
                                AndroidHttp.newCompatibleTransport(),
                                new GsonFactory(),
                                credential)
                                .setApplicationName("Drive API Migration")
                                .build();

                mDriveServiceHelper = new DriveServiceHelper(googleDriveService);
            })
            .addOnFailureListener(exception -> Log.e(TAG, "Unable to sign in.", exception));
}

这就是我传递 mDriveServiceHelper 的方式(它调用我需要的方法):

myIntent.putExtra("driver", (Parcelable) mDriveServiceHelper);

标签: androidgoogle-cloud-platformandroid-activitygoogle-drive-apiparcelable

解决方案


推荐阅读