YouTubePlayerView XML出现错误

4
我试图在适配器中放置一个YouTubePlayerView,但我没有成功。
我不想调用新的意图。我想在我的应用程序中显示视频。
我将YouTubeAndroidPlayerApi.jar放入我的libs文件夹中,并查看项目属性,一切都很好(我遵循了这个教程
这是我的XML代码和日志。
<LinearLayout
        android:id="@+id/ll_contentExercicios_LayoutVideoGif"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <com.google.android.youtube.player.YouTubePlayerView 
            android:id="@+id/vv_contentExercicios_YoutubeVideo"
            android:layout_width="300dp"
            android:layout_height="150dp" />


        <ImageView
            android:id="@+id/iv_contentExercicios_GifExercicio"
            android:layout_width="100dp"
            android:layout_height="150dp"
            android:layout_marginLeft="30dp" >
        </ImageView>
 </LinearLayout>


10-29 13:03:42.598: E/AndroidRuntime(4513): FATAL EXCEPTION: main
10-29 13:03:42.598: E/AndroidRuntime(4513): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.timo.atlas/br.com.timo.atlas.PrincipalActivity}: android.view.InflateException: Binary XML file line #121: Error inflating class com.google.android.youtube.player.YouTubePlayerView
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.os.Looper.loop(Looper.java:137)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.app.ActivityThread.main(ActivityThread.java:4895)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at java.lang.reflect.Method.invokeNative(Native Method)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at java.lang.reflect.Method.invoke(Method.java:511)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at dalvik.system.NativeStart.main(Native Method)
10-29 13:03:42.598: E/AndroidRuntime(4513): Caused by: android.view.InflateException: Binary XML file line #121: Error inflating class com.google.android.youtube.player.YouTubePlayerView
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at br.com.timo.atlas.adapters.ExerciciosAdapter.getView(ExerciciosAdapter.java:107)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at br.com.timo.atlas.adapters.MusculoAdapter.getView(MusculoAdapter.java:185)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at br.com.timo.atlas.PrincipalActivity.onCreate(PrincipalActivity.java:275)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.app.Activity.performCreate(Activity.java:5163)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
10-29 13:03:42.598: E/AndroidRuntime(4513):     ... 11 more
10-29 13:03:42.598: E/AndroidRuntime(4513): Caused by: java.lang.reflect.InvocationTargetException
10-29 13:03:42.598: E/AndroidRuntime(4513):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
10-29 13:03:42.598: E/AndroidRuntime(4513):     ... 23 more
10-29 13:03:42.598: E/AndroidRuntime(4513): Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity  which extends YouTubeBaseActivity as its context.
10-29 13:03:42.598: E/AndroidRuntime(4513):     at com.google.android.youtube.player.YouTubePlayerView.(Unknown Source)
10-29 13:03:42.598: E/AndroidRuntime(4513):     at com.google.android.youtube.player.YouTubePlayerView.(Unknown Source)
10-29 13:03:42.598: E/AndroidRuntime(4513):     ... 26 more

P.S.: 我正在阅读Youtube API文档,它说使用YouTubePlayerView必须扩展YouTubeBaseActivity,因此我知道我需要在BaseAdapter中创建YouTubePlayerView。

谢谢和问候。

4个回答

1

是的,您需要扩展YouTubeBaseActivity或者使用android.support.v4中的YouTubePlayerSupportFragment和FrameActivity。

<fragment
  android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
  android:id="@+id/youtube_fragment"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

1
您的适配器中的onBindViewHolder()方法应该像这样...
 @Override
public void onBindViewHolder(final VideoInfoHolder holder, final int position) {


    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);
        }
    };

    holder.youTubeThumbnailView.initialize(Resources.KEY, new YouTubeThumbnailView.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {

            youTubeThumbnailLoader.setVideo(VideoID[position]);
            youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
        }

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

在您的适配器中的viewHolder类中,实现以下方式的onclick监听器...

public class VideoInfoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    protected RelativeLayout relativeLayoutOverYouTubeThumbnailView;
    YouTubeThumbnailView youTubeThumbnailView;
    protected ImageView playButton;

    public VideoInfoHolder(View itemView) {
        super(itemView);
        playButton=(ImageView)itemView.findViewById(R.id.btnYoutube_player);
        playButton.setOnClickListener(this);
        relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
        youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail);
    }

    @Override
    public void onClick(View v) {

        Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) ctx, Resources.KEY, VideoID[getLayoutPosition()]);
        ctx.startActivity(intent);
    }
}

并且首先将您的小部件从

替换为


`<com.google.android.youtube.player.YouTubePlayerView 
            android:id="@+id/vv_contentExercicios_YoutubeVideo"
            android:layout_width="300dp"
            android:layout_height="150dp" />`

to

`<com.google.android.youtube.player.YouTubeThumbnailView
        android:id="@+id/youtube_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="centerCrop"
        android:visibility="visible"/>

`


0

YouTube 不允许在一个活动中使用多个 YouTube 播放器视图。 我不知道您的要求,但您可以使用带有片段的 Viewpager,并在片段内使用 childfragmentmanager 动态地将 youtubefragment 添加到您的片段中。


0
在你的Java文件ex. MainActivity.java中,尝试将public class MainActivity extends AppCompatActivity替换为public class MainActivity extends YouTubeBaseActivity

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接