“GoogleApiClient is not connected yet” exception in Cast application

前端 未结 4 635
暖寄归人
暖寄归人 2020-12-03 15:13

I\'m developing an Android application that casts content to Chromecast. Sometimes in my com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks i

相关标签:
4条回答
  • 2020-12-03 15:26

    Have you declare following <meta> tag

    <application ...> 
    ... 
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    ... 
    </application>
    

    I just forgot to write so may you also stuck with this reason.

    Thank you.

    0 讨论(0)
  • It seems like you are calling GoogleApiClient before it is connected

    move this line in onCreate()

    // First we need to check availability of play services
        if (checkPlayServices()) {
    
            // Building the GoogleApi client
            //buildGoogleApiClient();
            try {
                mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
    
                mGoogleApiClient.connect();
            }catch (IllegalStateException e)
            {
                Log.e("IllegalStateException", e.toString());
            }
            createLocationRequest();
        }
    

    Hope it helps you.

    0 讨论(0)
  • 2020-12-03 15:37

    From showcase app (Googlecast Github Sample CastHelloText-android ) receiver app is launched onRouteSelected (not onRouteAdded as you are doing in your code). I would try to change that. In case it does not work, I would add log lines in every method related to connection & session, and see what is happening.

    Another tip: I have had crash with stopping the application (in case chromecast device is physically plugged out from power). Solution is to putCast.CastApi.stopApplication(apiClient); inside if (apiClient.isConnected()).

    0 讨论(0)
  • 2020-12-03 15:50

    Google APIs for Android > GoogleApiClient

    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.

    The implementation of the GoogleApiClient appears designed for only a single instance. It's best to instantiate it only once in onCreate, then perform connections and disconnections using the single instance.

    I would guess that only one GoogleApiClient can be actually be connected, but multiple instances are receiving the onConnected callback.

    In your case it's probably fine to not call connect in onStart, but only in onRouteAdded.

    I think this issue is very similar to Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet

    0 讨论(0)
提交回复
热议问题