我阅读了其他类似的问题,但找不到解决方案。
我昨天实现了这个 https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi它工作正常。今天我不知道我在项目中搞砸了什么。当我输入 fragment 并且位置被禁用时,会弹出要求我打开位置的窗口,但无论我点击多少次“否”,它都不会消失,或者如果我点击"is",我将进入 logcat
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: FATAL EXCEPTION: main
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: Process: name.company.newapp, PID: 23652
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1000, result=-1, data=Intent { (has extras) }} to activity {name.company.newapp/name.company.newapp.HomeScreen}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:4054)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4097)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread.access$1400(ActivityThread.java:177)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1497)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5938)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.google.android.gms.internal.zzlh.zzb(Unknown Source)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.google.android.gms.internal.zzli.zzb(Unknown Source)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
基本上在 onActivityResult 中,googleClient
为空。正如我昨天所说的那样。 ..
测试位置 fragment :
public class TestLocation extends Fragment implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
public TestLocation() {
}
private static GoogleApiClient googleClient;
private Location mLastLocation;
private PendingResult<LocationSettingsResult> result;
private LocationRequest locationRequest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (googleClient==null) {
googleClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Defines the xml file for the fragment
View view = inflater.inflate(R.layout.upload_activity_fragment, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onConnected(Bundle bundle) {
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
result = LocationServices.SettingsApi.checkLocationSettings(googleClient, builder.build());
if (result != null) {
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a optionsDialog.
try {
// Show the optionsDialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
if (status.hasResolution()) {
status.startResolutionForResult(getActivity(), 1000);
}
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the optionsDialog.
break;
}
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode == Activity.RESULT_OK) && (requestCode == 1000)) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleClient);
if (mLastLocation == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleClient, locationRequest, this);
} else {
Log.d("askj","not null");
}
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
Log.e("location", "finally");
}
@Override
public void onResume() {
super.onResume();
if (googleClient!=null) {
googleClient.connect();
}
}
@Override
public void onPause() {
super.onPause();
if (googleClient!=null) {
if (googleClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(googleClient, this);
googleClient.disconnect();
}
}
}
}
最佳答案
来自 GoogleApiClient#disconnect()
的 Google API 引用方法:
public abstract void disconnect ()
Closes the connection to Google Play services. No calls can be made using this client after calling this method.
您正在 onPause()
方法中断开客户端连接,因此一旦您调用 startResolutionForResult()
,您的 GoogleApiClient
就会不再有效。您不需要像注销和重新注册 BroadcastReceiver
那样停止并重新启动客户端。从同一页面的顶部:
You should instantiate a client object in your Activity's
onCreate(Bundle)
method and then callconnect()
inonStart()
anddisconnect()
inonStop()
, regardless of the state.
关于android - 结果信息 - java.lang.IllegalStateException : GoogleApiClient is not connected yet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33747524/