android - Android Studio 布局上的 YoutubePlayer 错误

标签 android android-studio gradle layout youtube-api

我有一个必须显示 youtube 视频的 xml 文件,我使用以下示例:

https://www.sitepoint.com/using-the-youtube-api-to-embed-video-in-an-android-app/

当我制作 xml 文件时,我有这个异常(exception):

The following classes could not be instantiated: - com.google.android.youtube.player.YouTubePlayerView 

我把xml的代码放在下面:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.prodintec.am_motion.InformationActivity">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/ammotionlogocaberea"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="0dp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:text=""
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="0dp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:text=""
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="0dp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/imagenammotion" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:text=""
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="0dp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:text=""
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="0dp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/imagenammotion2" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

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

    </TableLayout>
</ScrollView>

</android.support.constraint.ConstraintLayout>

我有这样的 gradle 文件,我在 gradle 更改后重新同步:
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "com.prodintec.am_motion"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.google.android.gms:play-services-auth:10.2.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:cardview-v7:25.3.1'
compile fileTree(dir: 'libs', include: '*.jar')
compile files('libs/YouTubeAndroidPlayerApi.jar')
testCompile 'junit:junit:4.12'
}

我注册了 key :
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
    if (!wasRestored) {
        player.cueVideo("tipfCyqukrk"); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
    }
}

还有我的java类:
package com.prodintec.am_motion;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

public class InformationActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener{

TextView textoInicial;
ImageView cabecera;

private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_information);

    textoInicial = (TextView)findViewById(R.id.textView);
    textoInicial.setText("");
    cabecera = (ImageView)findViewById(R.id.imageView);
    cabecera.setScaleType(ImageView.ScaleType.FIT_XY);

    youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(Config.YOUTUBE_API_KEY, this);
}

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
    if (!wasRestored) {
        player.cueVideo("tipfCyqukrk"); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
    }
}

@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
        errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
    } else {
        String error = String.format(getString(R.string.player_error), errorReason.toString());
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RECOVERY_REQUEST) {
        // Retry initialization if user performed a recovery action
        getYouTubePlayerProvider().initialize(Config.YOUTUBE_API_KEY, this);
    }
}

protected Provider getYouTubePlayerProvider() {
    return youTubeView;
}
}

配置java代码:

公共(public)类配置{
private Config() {
}

public static final String YOUTUBE_API_KEY = "code value";

}

我不知道为什么会出现此错误,有人可以帮助我吗?

最佳答案

我修复了它,我必须将 Activity 的扩展属性更改为“YouTubeBaseActivity”,它工作得很好。

关于android - Android Studio 布局上的 YoutubePlayer 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44586702/

相关文章:

我的 Delphi XE7 应用程序的 Android 屏幕截图

java - 编译项目(路径: ':backend' ,配置: 'android-endpoints')错误

java - 尝试导入 2 个 jar 文件时,Android Studio 出现 DuplicateFileException

android - Gradle 永远下载 "builder-3.1.2.jar"

android - 为特定版本设置 debuggable true

android - 更新后java.nio.file.InvalidPathException

android-studio - 如何创建考虑了构建风格的任务

android - Rails 4 不接收 http post 内容,但在 Rails 3 中工作

android - 如何去除 PreferenceScreen 底部的水平线?

javascript - navigator.language 何时会与 navigator.languages[0] 不同?