Android:java.lang.IllegalStateException:必须连接GoogleApiClient

时间:2014-08-23 05:52:25

标签: android google-api

当我尝试登录Google Plus时,我正在我的Android应用中集成Google Plus,我的应用程序崩溃了。它说 -

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网站上注册了我的应用。

2 个答案:

答案 0 :(得分:-1)

首先检查它是否已连接,然后再进行操作。

GoogleApiClient mGoogleApiClient;
if(mGoogleApiClient.isConnected()){
  // good
}else{
  //connect it
  mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
}

答案 1 :(得分:-4)

重复:A Roundup of Popular JavaScript Date Parsing Libraries: XDate

无论如何,请将您的代码修改为:

@Override
    public void onConnected(Bundle connectionHint) {
        String personName = "Unknown";
        if (isSignedIn()) {
          if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null) {
            personName = Plus.AccountApi.getAccountName(mGoogleApiClient); 
          }
        }
}