YouTubeThumbnailView android studio中youtube视频的缩略图

时间:2018-08-14 10:20:22

标签: android-studio

我要访问YouTube YouTubeThumbnailView中的android studio视频的缩略图。

代码:

public static void thumbnailfunc(Activity context,final String VIDEO_ID, int ThumbnailID)
{
    YouTubeThumbnailView thumbnail =context.findViewById(R.id.ThumbnailID);

    thumbnail.initialize(API_KEY, new OnInitializedListener()
    {
        @Override
        public void onInitializationSuccess(
                YouTubeThumbnailView youTubeThumbnailView,
                final YouTubeThumbnailLoader youTubeThumbnailLoader)
        {
            youTubeThumbnailLoader.setVideo(VIDEO_ID);
            youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener()
            {
                @Override
                public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s)
                {
                    //need to release the loader!!!
                    youTubeThumbnailLoader.release();
                }

                @Override
                public void onThumbnailError(
                        YouTubeThumbnailView youTubeThumbnailView,
                        YouTubeThumbnailLoader.ErrorReason errorReason)
                {
                    //need to release the loader!!!
                    youTubeThumbnailLoader.release();
                }
            });
        }

        @Override
        public void onInitializationFailure(
                YouTubeThumbnailView youTubeThumbnailView,
                YouTubeInitializationResult youTubeInitializationResult)
        {
            //handle error here
        }
    });
}

这是我得到的错误:

Caused by: java.lang.NullPointerException:
  Attempt to invoke virtual method 'void
    com.google.android.youtube.player.YouTubeThumbnailView.initialize(
      java.lang.String,
      com.google.android.youtube.player.YouTubeThumbnailView$OnInitializedListener)'
    on a null object reference

1 个答案:

答案 0 :(得分:0)

您的日志报告清楚地表明,Android Studio无法在布局中找到缩略图视图 R.id.ThumbnailID 。请检查!