ResultInfo - java.lang.IllegalStateException:GoogleApiClient 尚未连接

ResultInfo - java.lang.IllegalStateException: GoogleApiClient is not connected yet

我阅读了其他类似的问题,但找不到解决方案。

我昨天实施了这个 https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi 并且运行良好。今天我不知道我在项目中搞砸了什么。当我输入片段并且位置被禁用时,会弹出 window 要求我打开位置,但无论我点击多少次“否”,它都不会消失,或者如果我点击“是”,我会进入 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.access00(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 为空。正如我昨天所说的那样。 ..

TestLocation 片段:

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();
                }
            }
        }
    }

来自 Google API 对 GoogleApiClient#disconnect() 方法的参考:

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 call connect() in onStart() and disconnect() in onStop(), regardless of the state.