Android Studio Layout上的YoutubePlayer错误

时间:2017-06-16 10:13:17

标签: 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文件时,我有这个例外:

threading.Thread(target=SysTrayIcon, args=("hazard.ico", hover_text, menu_options), kwargs=dict(on_quit=bye, default_menu_index=1))

我把xml的代码放在下面:

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

我有这样的gradle文件,并且在gradle上的更改后重新同步:

<?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>

我注册了以下关键字:

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'
}

我的java课程:

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

配置java代码:

public class Config {

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

}

我不知道为什么会有这个错误,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,我必须将活动的extends属性更改为&#34; YouTubeBaseActivity&#34;而且效果很好。