我正在将YouTube视频嵌入到我的Android应用中。我一直在阅读几个博客,帖子等,但我有些迷失了。我的布局是
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:theme="@style/Theme.AppCompat.NoActionBar"
tools:context=".UD1">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtubePlayerView"
android:layout_width="715dp"
android:layout_height="461dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
我创建的活动是:
public class UD1 extends YouTubeBaseActivity {
String APIkey = "aavvss";
String video1 = "hjFaqDNUVFo";
String video2 = "41cJ0mqWses";
String video3 = "uT8-YdXPNBk";
String video4 = "vSdAv931s-g";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ud1);
final YouTubePlayerView youtubePlayerView = findViewById(R.id.youtubePlayerView);
playVideo(video1, youtubePlayerView);
}
public void playVideo(final String videoId, YouTubePlayerView youTubePlayerView) {
//initialize youtube player view
youTubePlayerView.initialize(APIkey,new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo(videoId);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
}
运行应用程序时,出现以下异常:
原因:java.lang.IllegalStateException:YouTubePlayerView只能使用将YouTubeBaseActivity扩展为上下文的Activity来创建。
完整的堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: es.elena.learninggame, PID: 8501
java.lang.RuntimeException: Unable to start activity ComponentInfo{es.elena.learninggame/es.elena.learninggame.UD1}: android.view.InflateException: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: android.view.InflateException: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Error inflating class com.google.android.youtube.player.YouTubePlayerView
Caused by: android.view.InflateException: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Error inflating class com.google.android.youtube.player.YouTubePlayerView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:438)
at android.app.Activity.setContentView(Activity.java:3326)
at es.elena.learninggame.UD1.onCreate(UD1.java:28)
at android.app.Activity.performCreate(Activity.java:7783)
at android.app.Activity.performCreate(Activity.java:7772)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: 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:19)
E / AndroidRuntime:位于com.google.android.youtube.player.YouTubePlayerView。(未知来源:1) ...另外28个
但是,我的活动已经扩展了 YouTubeBaseActivity 。如前所述,我已经阅读了多个博客,包括StackOverflow,但我没有解决该错误。可以请任何人指导我吗?
预先感谢
答案 0 :(得分:1)
您的Activity中未正确进行Youtube初始化。像这样更改您的活动。
public class UD1 extends YouTubeBaseActivityextends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
String APIkey = "aassvv";
String video1 = "hjFaqDNUVFo";
String video2 = "41cJ0mqWses";
String video3 = "uT8-YdXPNBk";
String video4 = "vSdAv931s-g";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ud1);
final YouTubePlayerView youtubePlayerView = findViewById(R.id.youtubePlayerView);
// Initializing your video player like this
youtubePlayerView.initialize(APIkey, this);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
Toast.makeText(this, errorReason.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(video1);
// Hiding player controls
player.setPlayerStyle(PlayerStyle.CHROMELESS);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(APIkey, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtubePlayerView);
}
}
答案 1 :(得分:0)
最后,解决方案更加容易。在布局中,我使用的是主题
android:theme =“ @ style / Theme.AppCompat.NoActionBar”
嗯,通过删除主题,Youtube播放器可以完美加载。