GoogleApiClient 尚未连接,即使已调用 onConnected 并且我在 onCreate 中创建了 GoogleApiClient

3
我看了这个问题和答案:GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called。虽然它似乎与我的经验相似,但实际上不是。那位用户的问题在于他们在onStart()方法中声明了api客户端,而我像答案建议的那样在onCreate()方法中创建了我的api客户端。然而,我仍然遇到了同样的错误。
这是我为这三种方法编写的代码:
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Butter knife creates the variables */
    ButterKnife.bind(this);

    /* Startup location services */
    locationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    progressBar.setVisibility(View.INVISIBLE);

    refreshImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getForecast();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
    Log.i("Connected!!!", "WERE CONNECTED");
}

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }

    resumeLocationUpdates();

}
private void resumeLocationUpdates() {
    Log.i("RESUMING", "RESUMING LOCATION UPDATES");
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}

这是日志显示的内容,让我感到困惑的是它显示我们已经连接,但应用程序崩溃时却说我们没有连接...

-14 02:25:13.582 25885-25885/? I/Connected!!!: WERE CONNECTED
11-14 02:25:13.582 25885-25885/? I/RESUMING: RESUMING LOCATION UPDATES
11-14 02:25:13.582 25885-25885/? D/AndroidRuntime: Shutting down VM
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: FATAL EXCEPTION: main
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: Process: lpadron.me.weatherly, PID: 25885
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {lpadron.me.weatherly/lpadron.me.weatherly.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
2个回答

5

您的问题出现在onResume逻辑中:

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }

    resumeLocationUpdates();

}
private void resumeLocationUpdates() {
    Log.i("RESUMING", "RESUMING LOCATION UPDATES");
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}

mGoogleApiClient.connect() 的调用是异步的。在连接完成之前,它就返回了,而您在客户端连接之前请求位置更新。您需要将 requestLocationUpdates 调用移动到 GoogleApiClient.onConnected 回调中。此事件发生后,您的客户端已经连接。

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }   
}

@Override
public void onConnected(Bundle bundle) {
    resumeLocationUpdates();
}

2
  1. 你的类必须实现GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener接口,并重写所有方法。

  2. GoogleApiClient将与LocationServices通信并提供用户的纬度和经度。

  3. 在你的OnCreate()方法中,构建一个使用所需api的GoogleApiClient。

  4. 在你的onStart()方法中,开始连接GoogleApiClient。

  5. 当连接成功时,将调用OnConnected()方法,在这里我们将制作我们的LocationRequest。

  6. 一旦我们发出请求,就会调用onLocationChanged()方法,在此我们将使用位置对象连续获取纬度和经度更新。
  7. 当连接被暂停和连接失败时,将使用onConnectionSuspended(), onConnectionFailed()方法。
  8. 不要忘记在onStop()方法中断开GoogleApiClient连接,否则将浪费大量资源。

观看教程,请点击以下链接: 使用Google Play服务的Android位置API示例


1
我已经完成了所有这些。我采用了另一种解决方案,但是问题仍未得到解决。 - Luis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接