2017-06-16 42 views
0

我有要显示YouTube视频的XML文件,我用这个例子:YoutubePlayer错误

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

当我做了xml文件,我有这样的例外:

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> 

我有GR adle文件中像这样我的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' 
} 

我注册的关键白衣说:

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 class Config {

private Config() { 
} 

public static final String YOUTUBE_API_KEY = "code value"; 

}

我不知道为什么我有这样的错误,谁能帮助我?

+0

添加您的java类 – Pavya

+0

好的,请等一下。我现在添加它。 – Imrik

+0

@Pravin添加我的java类:) – Imrik

回答

0

我修正了它,我必须将活动的扩展属性更改为“YouTubeBaseActivity”,并且它完美地工作。