【问题标题】:GoogleApiClient is not connected yet, even though onConnected is called and I'm creating my GoogleApiClient in onCreateGoogleApiClient 尚未连接,即使调用了 onConnected 并且我正在 onCreate 中创建我的 GoogleApiClient
【发布时间】:2015-11-14 07:40:12
【问题描述】:

我在这里查看了这个问答:GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called

因为它似乎与我所经历的相似,但事实并非如此。该用户的问题是他们在 onStart() 方法中声明了他们的 api 客户端,我在 onCreate() 方法中创建了我的,就像答案所建议的那样。但是,我仍然遇到同样的错误。

这是我为这三种方法提供的代码:

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.

【问题讨论】:

    标签: java android location google-api-client


    【解决方案1】:

    您的问题出在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 连接,否则会浪费大量资源。

      在这里观看教程 Android Location API Using Google Play Services

      【讨论】:

      • 我已经完成了所有这些。我用了另一个解决方案,这个问题仍然没有解决。
      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多