Alan Rochford Alan Rochford - 6 months ago 143
Android Question

A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context

I'm getting this error is my XML file but can't figure it out as my Activity already extends YouTubeBaseActivity. Can anybody help me?

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp" />


Activity:

public class UserHomepage extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener


Stack Trace

java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)

Answer

Since all other similar exception happened are due to not extending YoutubeBaseActivity. This is probably a bug, probably the layout inflater tries to instantiate the view passing Activity context instead YoutubeBaseActivity.

Take 0:

Check if you are using the latest version of sdk, tools and youtube api.

Take 1:

Put a FrameLayout with same attributes and later add the YoutubePlayerView programatically. Simple FrameLayout should not contribute much towards your view hierarchy. Anyways, give it a try.

Take 2: Use fragment version of the player.