Google Api Client sometime NULL in onConnected

前端 未结 6 417
小蘑菇
小蘑菇 2021-01-03 23:51

I implement GoogleApiClient as bellow:

 mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, 0 /* client         


        
相关标签:
6条回答
  • 2021-01-04 00:31

    build() calls onConnected immediately if you are already connected. Therefore, your variable might be null.

    Better use

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, 0 /* clientId */, this)
                .addApi(LocationServices.API)
                .addApi(Places.GEO_DATA_API)
                .addConnectionCallbacks(this);
    mGoogleApiClient.build();
    
    0 讨论(0)
  • 2021-01-04 00:36

    I had the same problem. All I did to solve it is remove .enableAutoManage(this, 0 /* clientId */, this) because it just doesn't work properly from what I assumed. Then, override these methods in your activity:

    @Override
    public void onStart() {
        super.onStart();
        if (mGoogleApiClient != null) {
            mGoogleApiClient.connect();
        }
    }
    
    @Override
    public void onStop() {
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }
    

    Technically, that is what .enableAutoManage(this, 0 /* clientId */, this) was supposed to do, except that now, everything works as it should.

    0 讨论(0)
  • 2021-01-04 00:37

    If u are facing this problem when u try to reinitialize mGoogleApiClient, then just remove

    .enableAutoManage(this, 0 /* clientId */, this)

    Use

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addApi(Places.GEO_DATA_API)
                .addConnectionCallbacks(this)
                .build();
    

    and it will work fine

    0 讨论(0)
  • 2021-01-04 00:39

    I think you'd better watch this reference.

    reference page of "public GoogleApiClient.Builder enableAutoManage"

    In this page shows that, through IllegalStateException if clientId is already being auto-managed. So, check on your code with

                    .enableAutoManage(this, 0 /* clientId */, this)
    

    I think if exception on your code, it could return zero as not completed.

    0 讨论(0)
  • 2021-01-04 00:40

    I had the same problem (Already managing a GoogleApiClient with id 0) in a fragment, and finally I resolved it :

    • Override onStart() and onStop() normally
    • Add in onStop() call yourApiGoogle.stopAutoManage(context);

    Have a nice day...

    0 讨论(0)
  • 2021-01-04 00:48

    Documentation says: At any given time, only one auto-managed client is allowed per id. To reuse an id you must first call stopAutoManage(FragmentActivity) on the previous client.

    What I personally do is making a call to bellow method before I leave the activity, in which I am using the Google Api Client.

    private void stopAutoManage() {
        if (mGoogleApiClient != null)
            mGoogleApiClient.stopAutoManage(mActivity);
    }
    
    0 讨论(0)
提交回复
热议问题