当我试图登录Google Plus时,我正在将Google Plus集成到我的android应用程序中,我的应用程序刚刚崩溃。它说-
java.lang.IllegalStateException: GoogleApiClient must be connected
下面是stacktrace指向的行号,
if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null)
@Override
public void onConnected(Bundle connectionHint) {
String personName = "Unknown";
if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null) {
personName = Plus.AccountApi.getAccountName(mGoogleApiClient);
}
}
我已经完成了整合的所有步骤,将Google play服务添加到我的项目中,并在Google API网站上注册了我的应用。
首先检查它是否连接,然后做你的事情。
GoogleApiClient mGoogleApiClient;
if(mGoogleApiClient.isConnected()){
// good
}else{
//connect it
mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
}
副本:Android: Google play games services connection error (java.lang.IllegalStateException: GoogleApiClient must be connected.)
无论如何,修改你的代码为:
@Override
public void onConnected(Bundle connectionHint) {
String personName = "Unknown";
if (isSignedIn()) {
if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null) {
personName = Plus.AccountApi.getAccountName(mGoogleApiClient);
}
}
}