gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:13 24 4
gpt4 key购买 nike

我在这里查看了这个问答: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.

最佳答案

您的问题出在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();
}

关于java - GoogleApiClient 尚未连接,即使调用了 onConnected 并且我正在 onCreate 中创建我的 GoogleApiClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33706261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com