android - 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);
解决方案
推荐阅读
- html - 我如何从另一个页面运行 html 代码(例如,我有一个 html 文件并引用它)
- sql - 计算 BOM 项目的理论可用数量
- php - 在 Visual Studio Code 中使用 Xdebug 调试 php 时出现“错误评估代码”
- flask - 我已经尝试了所有在 Flask 中禁用 css 缓存的常用方法,但它们都不适合我
- javascript - 将下拉列表中的项目导入 SharePoint 列表
- python - 变量输出上的 Sympy 替换
- c# - 使用 AddAutomationFocusChangedEventHandler 进行焦点跟踪会在 WPF 中触发多个事件
- python - 将其转换为 xlsx 后如何压缩(单个 zip 文件)多个熊猫数据帧?
- parallel-processing - 区分 SIMD 和 MIMD 的实际应用示例有哪些
- reactjs - 在 ReactJS 中集成 Mapbox 的错误