mGoogleApiClient.isConnected()
旧的代码中,这句代码报出异常状态错误。导致接下来的new GoogleApiClient.Builder(activity)报错GoogleApiClient.getContext()' on a null object reference
首先找了一个api,发现实现方式已经过时。
mSignInClient=GoogleSignIn.getClient(activity, mGoogleSignInOptions);
取代了GoogleApiClient
public class MyNewActivity extends AppCompatActivity {
private static final int RC_SIGN_IN = 9001;
private GoogleSignInClient mSignInClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleSignInOptions options =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.build();
mSignInClient = GoogleSignIn.getClient(this, options);
}
private void signIn() {
// Launches the sign in flow, the result is returned in onActivityResult
Intent intent = mSignInClient.getSignInIntent();
startActivityForResult(intent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task =
GoogleSignIn.getSignedInAccountFromIntent(data);
if (task.isSuccessful()) {
// Sign in succeeded, proceed with account
GoogleSignInAccount acct = task.getResult();
} else {
// Sign in failed, handle failure and update UI
// ...
}
}
}
}
然后就报错12501错误,其实这个码在api里是用户取消的意思
但是人家没爆出来给我们用。
没办法,只能用activityResult方法返回的resultCode字段来判断是否为取消行为了。投机取消的解决这个问题。










网友评论