java.lang.IllegalStateException:未连接。调用connect()并等待onConnected()被调用

时间:2016-02-09 10:05:30

标签: android

我想在RecyclerView中显示垂直列表。每个列表行包含多个YoutubeThumbnailView。为了初始化,我创建了一个单独的类,它将参数作为YoutubethumbnailView和视频键的引用。它给出了例外。

这是我的Recycler Adapter代码

public class DashBoardRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 1;
private static final int TYPE_ITEM = 2;
ThumbnailImageView thumbnailImageView;

private ArrayList<VideoDetailsModel> nonCateList;
private Activity activity;
private ArrayList<ArrayList<DashBoardModel>> mainContent;

public DashBoardRecyclerAdapter(Activity activity,ArrayList<VideoDetailsModel> nonCateList,ArrayList<ArrayList<DashBoardModel>> mainContent) {
    this.activity = activity;
    this.nonCateList = nonCateList;
    this.mainContent = mainContent;
}
public class DashViewHolder extends RecyclerView.ViewHolder{
    private TextView suCategoryName;
    private TextView more;
    private TextView videoTitle;
    private TextView videoTitle2;
    private TextView videoDescription;
    private TextView videoDescription2;
    private YouTubeThumbnailView youTubeThumbnailViewOne;
    private YouTubeThumbnailView getYouTubeThumbnailViewTwo;

    public DashViewHolder(View itemView) {
        super(itemView);
        suCategoryName = (TextView)itemView.findViewById(R.id.dash_subcate_title);
        more = (TextView)itemView.findViewById(R.id.dash_more);
        videoTitle = (TextView)itemView.findViewById(R.id.dash_video_title);
        videoTitle2 = (TextView)itemView.findViewById(R.id.dash_video_title2);
        getYouTubeThumbnailViewTwo = (YouTubeThumbnailView)itemView.findViewById(R.id.dash_thumbnail_two);
        youTubeThumbnailViewOne = (YouTubeThumbnailView)itemView.findViewById(R.id.dash_thumbnail_one);
    }
}
public class HeaderViewHolder extends RecyclerView.ViewHolder{
    private TextView single_image_title,double_image_title,double_image_title2,more_item_title;
    YouTubeThumbnailView singleView,doubleView1,doubleView2,thirdView;
    public HeaderViewHolder(View itemView) {
        super(itemView);
        single_image_title = (TextView)itemView.findViewById(R.id.single_video_title);
        double_image_title = (TextView)itemView.findViewById(R.id.two_video_title);
        double_image_title2  = (TextView)itemView.findViewById(R.id.two_video_title2);
        more_item_title = (TextView)itemView.findViewById(R.id.more_video_title);
        singleView = (YouTubeThumbnailView)itemView.findViewById(R.id.single_video_dash_thumbnail);
        doubleView1 = (YouTubeThumbnailView)itemView.findViewById(R.id.two_video_thumbnail_one);
        doubleView2 = (YouTubeThumbnailView)itemView.findViewById(R.id.two_video_thumbnail_two);
        thirdView = (YouTubeThumbnailView)itemView.findViewById(R.id.third_thumbnail);

    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

    if(holder instanceof HeaderViewHolder){
        HeaderViewHolder headerView = (HeaderViewHolder)holder;


    }else {
        DashViewHolder holder1 = (DashViewHolder) holder;
        final ArrayList<DashBoardModel> content = mainContent.get(position-1);
        holder1.suCategoryName.setText(content.get(position-1).getSubCategoryName());
        holder1.videoTitle.setText(content.get(position-1).getVideoDetailsModels().get(0).getVideoTitle());
        // holder1.videoDescription.setText(dashBoardModels.get(position).getVideoDetailsModels().get(position).getDescription());
        holder1.videoTitle2.setText(content.get(position-1).getVideoDetailsModels().get(1).getVideoTitle());
        // holder1.videoDescription2.setText(dashBoardModels.get(position).getVideoDetailsModels().get(position+1).getDescription());
        thumbnailImageView = new ThumbnailImageView(holder1.youTubeThumbnailViewOne,content.get(position-1).getVideoDetailsModels().get(0).getVideoId());
        thumbnailImageView = new ThumbnailImageView(holder1.youTubeThumbnailViewOne,content.get(position-1).getVideoDetailsModels().get(1).getVideoId());
    }

}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if(viewType == TYPE_HEADER){
        if(nonCateList.size()==1){
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_video_layout,parent,false);
        return new HeaderViewHolder(v);
        }else if(nonCateList.size()==2){
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.two_video_layout,parent,false);
            return new HeaderViewHolder(v);
        }
        else if(nonCateList.size()>2){
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_video_with_more,parent,false);
            return new HeaderViewHolder(v);

        }
    }else if(viewType == TYPE_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.dash_board, parent, false);
        return new DashViewHolder(v);
    }
    return null;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public int getItemCount()
{
    return mainContent.size()+1;
}
private boolean isPositionHeader (int position) {
    return position == 0;
}

    @Override
public int getItemViewType(int position) {
        if(isPositionHeader (position)) {
            return TYPE_HEADER;
        }
        return TYPE_ITEM;

    }

}

这是我的ThumbnailImageView.java类

public class ThumbnailImageView {
private YouTubeThumbnailView youTubeThumbnailView;
String videoKey;

public ThumbnailImageView(YouTubeThumbnailView youTubeThumbnailView, String videoKey) {
    this.youTubeThumbnailView = youTubeThumbnailView;
    this.videoKey = videoKey;
    initializeThumbNail();
}

private void initializeThumbNail() {
    final YouTubeThumbnailLoader.OnThumbnailLoadedListener onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
        @Override
        public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {

        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
            youTubeThumbnailView.setVisibility(View.VISIBLE);
            //holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
        }
    };

    youTubeThumbnailView.initialize(new Constant().YOU_TUBE_API_KEY, new YouTubeThumbnailView.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {

            youTubeThumbnailLoader.setVideo(videoKey);
            youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
        }

        @Override
        public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
            //write something for failure
        }
    });
}

这是我的logcat消息

FATAL EXCEPTION: main
  Process: com.dp.videostore, PID: 2200
  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)
at com.google.android.youtube.player.internal.r.g(Unknown Source)
at com.google.android.youtube.player.internal.r$c.a(Unknown Source)
at com.google.android.youtube.player.internal.r$b.a(Unknown Source)
at com.google.android.youtube.player.internal.r$a.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

我有同样的问题 &#34; YouTubeThumbnailView Not connected. Call connect() and wait for onConnected() to be called&#34; 在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("aaaaaaaaa", 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;
        }
    });
}

这对我有用......