【问题标题】:ResultInfo - java.lang.IllegalStateException: GoogleApiClient is not connected yetResultInfo - java.lang.IllegalStateException: GoogleApiClient 尚未连接
【发布时间】:2015-11-17 01:12:04
【问题描述】:

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

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

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

【问题讨论】:

  • 尝试从GoogleApiClient 声明中删除static 修饰符。
  • 我删除了,但没有解决问题
  • 您是否创建了多个TestLocation Fragment 实例?
  • 什么意思?我只在按下按钮实例化它时使用它。如果位置在一切正常,但当位置关闭时就会出现问题

标签: android android-fragments google-api-client


【解决方案1】:

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

public abstract void disconnect()

关闭与 Google Play 服务的连接。调用此方法后,无法使用此客户端进行调用。

您正在使用onPause() 方法断开客户端连接,因此一旦您调用startResolutionForResult(),您的GoogleApiClient 就不再有效。您无需像取消注册并重新注册BroadcastReceiver 那样停止并重新启动客户端。从同一页面的顶部:

您应该在 Activity 的 onCreate(Bundle) 方法中实例化一个客户端对象,然后在 onStart() 中调用 connect() 并在 onStop() 中调用 disconnect(),无论状态如何。

【讨论】:

  • 非常感谢。以后我会更加关注fragment的生命周期。
  • 没问题。我不得不检查自己。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
相关资源
最近更新 更多