GoogleApiClient 尚未连接,即使调用了 onConnected 并且我正在 onCreate 中创建我的 GoogleApiClient
GoogleApiClient is not connected yet, even though onConnected is called and I'm creating my GoogleApiClient in onCreate
我在这里查看了这个问答:
因为它似乎与我所经历的相似,但事实并非如此。该用户的问题是他们在 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.
您的 class 必须实现 GoogleApiClient.ConnectionCallbacks、GoogleApiClient.OnConnectionFailedListener、LocationListener 并覆盖所有方法。
GoogleApiClient 将与 LocationServices 通信并为您提供用户的纬度和经度。
在您的 OnCreate() 方法中构建一个使用所需 api.
的 GoogleApiClient
在您的 onStart() 方法中开始连接 GoogleApiClient。
在成功连接时调用 OnConnected() 方法,我们将在其中发出 LocationRequest。
- 一旦我们发出请求,就会调用 onLocationChanged() 方法,我们将在其中使用位置对象来持续获取纬度和经度更新。
- onConnectionSuspended(),onConnectionFailed()会在连接挂起和连接失败时使用
- 不要忘记在 onStop() 方法中断开 GoogleApiClient 连接,否则会浪费大量资源。
你的问题出在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()
的调用是异步的。它 returns 在连接完成之前,并且您在客户端连接之前请求位置更新。您需要将 requestLocationUpdates
调用移至 GoogleApiClient.onConnected
回调。在此事件之后,您的客户端已连接。
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
resumeLocationUpdates();
}
我在这里查看了这个问答:
因为它似乎与我所经历的相似,但事实并非如此。该用户的问题是他们在 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.
您的 class 必须实现 GoogleApiClient.ConnectionCallbacks、GoogleApiClient.OnConnectionFailedListener、LocationListener 并覆盖所有方法。
GoogleApiClient 将与 LocationServices 通信并为您提供用户的纬度和经度。
在您的 OnCreate() 方法中构建一个使用所需 api.
的 GoogleApiClient
在您的 onStart() 方法中开始连接 GoogleApiClient。
在成功连接时调用 OnConnected() 方法,我们将在其中发出 LocationRequest。
- 一旦我们发出请求,就会调用 onLocationChanged() 方法,我们将在其中使用位置对象来持续获取纬度和经度更新。
- onConnectionSuspended(),onConnectionFailed()会在连接挂起和连接失败时使用
- 不要忘记在 onStop() 方法中断开 GoogleApiClient 连接,否则会浪费大量资源。
你的问题出在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()
的调用是异步的。它 returns 在连接完成之前,并且您在客户端连接之前请求位置更新。您需要将 requestLocationUpdates
调用移至 GoogleApiClient.onConnected
回调。在此事件之后,您的客户端已连接。
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
resumeLocationUpdates();
}