错误:GoogleApiClient尚未连接,而onRequestPermissionsResult

时间:2016-09-08 14:30:31

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

在firebase崩溃报告中,我从API级别23上的一个设备收到此致命错误。 我一直试图让我的设备重新创建错误,但没有誓言。任何人都可以指出我的问题可能是什么?以下是相关的代码部分:

Exception java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=0, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.xyz.xyz/com.xyz.xyz.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
android.app.ActivityThread.deliverResults (ActivityThread.java:3758)
android.app.ActivityThread.handleSendResult (ActivityThread.java:3801)
android.app.ActivityThread.access$1400 (ActivityThread.java:157)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1405)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5551)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:730)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:620)
arrow_drop_down
Caused by java.lang.IllegalStateException: GoogleApiClient is not connected yet.
com.google.android.gms.c.att.b ()
com.google.android.gms.c.aul.b ()
com.google.android.gms.c.auf.b ()
com.google.android.gms.location.internal.d.a ()
com.xyz.xyz.MainActivity.n ()
com.xyz.xyz.MainActivity.onRequestPermissionsResult ()
android.app.Activity.dispatchRequestPermissionsResult (Activity.java:6588)
android.app.Activity.dispatchActivityResult (Activity.java:6467)
android.app.ActivityThread.deliverResults (ActivityThread.java:3754)
android.app.ActivityThread.handleSendResult (ActivityThread.java:3801)
android.app.ActivityThread.access$1400 (ActivityThread.java:157)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1405)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5551)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:730)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:620)

的OnCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
 ...
       // Create an instance of GoogleAPIClient.
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .addApi(AppIndex.API).build();
        }

}

onStart()

protected void onStart() {
    Log.d(TAG, "onStart() called");
    super.onStart();

    mGoogleApiClient.connect();
...
}

的onResume()

@Override
public void onResume() {
    super.onResume();
    // Within onPause(), we pause location updates, but leave the
    // connection to GoogleApiClient intact.  Here, resume receiving
    // location updates if the user has requested them.
    if (mGoogleApiClient.isConnected()) startLocationUpdates();

的onPause()

@Override
protected void onPause() {
    // Stop location updates to save battery, but don't disconnect the GoogleApiClient.
    if (mGoogleApiClient.isConnected()) stopLocationUpdates();

    super.onPause();

的onStop()

@Override
protected void onStop() {

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }

}

onDestory()

public void onDestroy() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {

            Log.d(TAG, "onDestroy() disconnecting mGoogleApiClient");

            mGoogleApiClient.disconnect();
        }
        super.onDestroy();
}

最后是活动类

    protected void startLocationUpdates() {
               if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
    }

    /**
     * Removes location updates from the FusedLocationApi.
     */
    protected void stopLocationUpdates() {        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
   }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        switch (requestCode) {
            case REQUEST_LOCATION:
                // BEGIN_INCLUDE(permission_result)
                // Received permission result for LOCATION permission.

                Log.i(TAG, "Received response for LOCATION permission request.");

                // Check if the only required permission has been granted
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.i(TAG, "LOCATION permission has now been granted.");

                    Snackbar.make(mLayout, R.string.permision_location_granted,
                            Snackbar.LENGTH_SHORT).show();

                    setMyLocation();


                } else {
                    Log.w(TAG, "LOCATION permission was NOT granted. Setting city fixed location");

                    Snackbar.make(mLayout, R.string.permissions_not_granted,
                            Snackbar.LENGTH_SHORT).show();
                    setMyLocation();

                }
                // END_INCLUDE(permission_result)

            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private void setMyLocation() {
        Log.d(TAG, "setMyLocation() called");
        Location lastLocation = null;
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            lastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (lastLocation == null) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            }


        }
        if (lastLocation == null) {
            lastLocation = new Location("fixed");
            lastLocation.setLatitude(45.5017);
            lastLocation.setLongitude(-73.5673);
            lastLocation.setTime(System.currentTimeMillis() / 1000L);
        }
        updateLocation(lastLocation);
    }

0 个答案:

没有答案