java.lang.IllegalStateException:未连接。致电connect() - YouTube API

时间:2016-02-24 15:36:46

标签: java android android-youtube-api

我正在尝试将ThumbnailView用于我的ArrayAdapter但是我收到以下错误:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
        at com.google.android.youtube.player.internal.r.i(Unknown Source)
        at com.google.android.youtube.player.internal.o.k(Unknown Source)
        at com.google.android.youtube.player.internal.o.a(Unknown Source)
        at com.google.android.youtube.player.internal.p.<init>(Unknown Source)
        at com.google.android.youtube.player.internal.ac.a(Unknown Source)
        at com.google.android.youtube.player.YouTubeThumbnailView$a.a(Unknown Source)

我的ArrayAdapter看起来像这样:

Code for ArrayAdapter

我的xml video_items.xml看起来像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/listviewbox"
    android:padding="10dp"
    android:layout_height="match_parent">

    <!-- Video Thumbnail -->
    <com.google.android.youtube.player.YouTubeThumbnailView
        android:id="@+id/imageView_thumbnail"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:layout_alignParentLeft="true"
        android:layout_margin="5dp"
        android:background="@color/panelcolor"
        android:scaleType="centerCrop" />

    <!-- Video Title -->
    <TextView
        android:id="@+id/video_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView_thumbnail"
        android:layout_toRightOf="@+id/imageView_thumbnail"
        android:paddingBottom="5dp"
        android:paddingRight="5dp"
        android:text="@string/videotext"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

</RelativeLayout>

这里有什么问题?

2 个答案:

答案 0 :(得分:4)

在长ListView中显示YoutubeThumbnailView时,我也发生了同样的事情。我找到了提到的解决方案f.e.这里: https://github.com/youtube/yt-android-player/issues/2#issuecomment-58913900 或者在这里: https://code.google.com/p/gdata-issues/issues/detail?id=7533#c6

解决方案是将YoutubeThumbnailView包装在ViewHolder对象中,跟踪其初始化状态。 initialize(..)方法可能无法多次调用。

不幸的是,例外情况不会很快导致。

答案 1 :(得分:0)

指的是:java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called

我有同样的问题 “YouTubeThumbnailView Not connected. Call connect() and wait for onConnected() to be called” 在RecyclerView.Adapter中快速滚动,recylerView中有3个youtube缩略图。

在我这样放的适配器中: 字段:private boolean readyForLoadingYoutubeThumbnail = true; 并在onBindViewHolder

/**
 * https://issuetracker.google.com/issues/35176819#c6
 * https://github.com/youtube/yt-android-player/issues/2#issuecomment-58913900
 * YouTubeThumbnailView Not connected. Call connect() and wait for onConnected() to be called
 */
Log.d(TAG, "readyForLoadingYoutubeThumbnail" + readyForLoadingYoutubeThumbnail);
final YouTubeInitializationResult result = YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(activity);
if (readyForLoadingYoutubeThumbnail) {
    Log.d(TAG, "initializing for youtube thumbnail view...");
    readyForLoadingYoutubeThumbnail = false;
    holder.youTubeThumbnailView.initialize("aaaaaaaaaaaaa", new YouTubeThumbnailView.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(final YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {

            youTubeThumbnailLoader.setVideo(videoId);
            youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener(){

                @Override
                public void onThumbnailLoaded(YouTubeThumbnailView childYouTubeThumbnailView, String s) {
                    childYouTubeThumbnailView.setVisibility(View.VISIBLE);
                    holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
                    youTubeThumbnailLoader.release(); // spy ga memory lick
                }

                @Override
                public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
                    youTubeThumbnailLoader.release(); // spy ga memory lick
                }
            });
            youTubeThumbnailView.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY);

            readyForLoadingYoutubeThumbnail = true;
        }

        @Override
        public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
            //do nohing.. ada error, tambahin method ini jalan, error-nya lupa...
            readyForLoadingYoutubeThumbnail = true;
        }
    });
}

这对我有用......