YouTube api: Not connected. Call connect() and wait for onConnected() to be called

I am using YouTubeFragment in an Android app and getting following crash on Android 4+ devices.

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.youtube.player.a.at.i(Unknown Source)
at com.google.android.youtube.player.a.an.k(Unknown Source)
at com.google.android.youtube.player.a.an.a(Unknown Source)
at com.google.android.youtube.player.a.ao.<init>(Unknown Source)
at com.google.android.youtube.player.a.f.a(Unknown Source)
at com.google.android.youtube.player.q.a(Unknown Source)
at com.google.android.youtube.player.a.at.g(Unknown Source)
at com.google.android.youtube.player.a.ax.a(Unknown Source)
at com.google.android.youtube.player.a.aw.a(Unknown Source)
at com.google.android.youtube.player.a.av.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5450)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)

The fragment is being started like this:

  • How to check if APK is signed or “debug build”?
  • Android: How to initialize a variable of type “Location” (other than making it equal to null)
  • Volley JsonObjectRequest Post request not working
  • What is the difference in contacts versus raw-contacts?
  • Activity restart on rotation Android
  • Scroll doesn't work in NestedScrollView when try to scroll from views with click events
  •  XYouTubeFragment yFragment = XYouTubeFragment.newInstance(ytVideoId);
     activity.commitFragment(yFragment);
    

    and defined like this:

    public class XYouTubeFragment extends YouTubePlayerSupportFragment {
    
        public static XYouTubeFragment newInstance(String url) {
            XYouTubeFragment youTubeFrag = new XYouTubeFragment();
            Bundle bundle = new Bundle();
            bundle.putString("url", url);
            youTubeFrag.setArguments(bundle);
            youTubeFrag.init();
            return youTubeFrag;
        }
    
        private void init() {
    
            initialize(Constants.YOUTUBE_API_KEY, new OnInitializedListener() {
    
                @Override
                public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { }
    
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
                    player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                    if (!wasRestored) {
                        player.loadVideo(getArguments().getString("url"), 0);
                    }
                }
            });
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            MainActivity activity = (MainActivity) getActivity();
            activity.getSupportActionBar().hide();
            activity.findViewById(R.id.alert_list).setVisibility(View.GONE);
        }
    }
    

    Any idea what could be wrong?

  • Is there ANY android emulator that supports microphone input?
  • Referencing the user's home directory in a Gradle script
  • Setting Elevation in XML on AppCompat CardView on Android 5.0
  • Converting Epoch time to date string
  • Android - switching between landscape and portrait mode makes Intent lose values
  • Get position of imageview on layout android
  • One Solution collect form web for “YouTube api: Not connected. Call connect() and wait for onConnected() to be called”

    public class YoutubeManager implements YouTubePlayer.OnInitializedListener {
    
    
            String VideoTitle;
            String VideoIdLoaded;
            boolean videoTitleEnabled;
            boolean isPreLoaded;
    
            public void loadVideoWithId(String videoId) {
    
            }
    
        public static final String DEFAULT_API_KEY = "Your api key";
        String appKey;
        String searchKey;
        String videoId_local;
        String userId_local;
        boolean firstRun;
         Activity mActivity;
        YouTubePlayer mYoutubePlayer;
        private YouTubeFragment youTubePlayerFragment;
        private MyPlayerStateChangeListener myPlayerStateChangeListener;
        private MyPlaybackEventListener myPlaybackEventListener;
    
        public YoutubeManager(Activity activity, String key, int containerId, int windowWidth, int windowHeight) {
            mActivity = activity;
            appKey = key;
            searchKey = null;
            videoId_local = null;
            userId_local = null;
            firstRun = true;
            offsetTime = 0D;
            myPlayerStateChangeListener = new MyPlayerStateChangeListener();
            myPlaybackEventListener = new MyPlaybackEventListener();
            FragmentManager fragmentManager = activity.getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            YouTubePlayerFragment = new YouTubeFragment();
            ViewGroup.MarginLayoutParams params =
                    new ViewGroup.MarginLayoutParams(windowWidth / 2,
                            windowHeight / 2);
            params.setMargins(0, 0, 0, 0);
            youTubePlayerFragment.setLayoutParams(params);
            fragmentTransaction.add(containerId, youTubePlayerFragment);
            fragmentTransaction.commit();
            youTubePlayerFragment.initialize(key, this);
        }
    
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
                                            boolean wasRestored) {
            mYoutubePlayer = player;
            mYoutubePlayer.setPlayerStateChangeListener(myPlayerStateChangeListener);
            mYoutubePlayer.setPlaybackEventListener(myPlaybackEventListener);
            if (!wasRestored) {
                mYoutubePlayer.cueVideo("nCgQDjiotG0");
            } else {
                //TEST of video playback
                mYoutubePlayer.cueVideo("nCgQDjiotG0");
                play(null);
            }
        }
    
        public void onInitializationFailure(YouTubePlayer.Provider provider,
                                            YouTubeInitializationResult errorReason) {
            if (errorReason.isUserRecoverableError()) {
                //errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
            } else {
                //String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
                //Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
            }
        }
    
        public void loadVideoWithId(String videoToLoad) {
            videoId_local = videoToLoad;
            mYoutubePlayer.cueVideo(videoId_local);
        }
    
        //Users
        public String getLocalUserID() {
            return videoId_local;
        }
    
        ;
    
        public void addUser(String userId) {
    
        }
    
        ;
    
        public void removeUser(String userId) {
    
        }
    
        public void removeAllUsers() {
    
        }
    
        //External Playback
        public void play(String userId) {
            mYoutubePlayer.play();
        }
    
        public void pause(String userId) {
            mYoutubePlayer.pause();
        }
    
        public void stop(String userId) {
    
        }
    
        public void close(String userId) {
    
        }
    
        private final class MyPlayerStateChangeListener implements PlayerStateChangeListener {
    
            @Override
            public void onAdStarted() {
                Log.INFO("YOUTUBE", "onAdStarted()");
            }
    
            @Override
            public void onError(
                    com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0) {
                Log.INFO("YOUTUBE", "onError(): " + arg0.toString());
            }
    
            @Override
            public void onLoaded(String arg0) {
                Log.INFO("YOUTUBE", "onLoaded(): " + arg0);
            }
    
            @Override
            public void onLoading() {
                Log.INFO("YOUTUBE", "onLoading()");
            }
    
            @Override
            public void onVideoEnded() {
                Log.INFO("YOUTUBE", "onVideoEnded()");
            }
    
            @Override
            public void onVideoStarted() {
                Log.INFO("YOUTUBE", "onVideoStarted()");
            }
    
        }
    
        private final class MyPlaybackEventListener implements PlaybackEventListener {
    
            @Override
            public void onBuffering(boolean arg0) {
                Log.INFO("YOUTUBE", "onBuffering(): " + String.valueOf(arg0));
            }
    
            @Override
            public void onPaused() {
    
            }
    
            @Override
            public void onPlaying() {
    
            }
    
            @Override
            public void onSeekTo(int arg0) {
    
            }
    
            @Override
            public void onStopped() {
    
            }
    
        }
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.