无法实例化 com.google.android.youtube.player.YouTubePlayerView

Posted

技术标签:

【中文标题】无法实例化 com.google.android.youtube.player.YouTubePlayerView【英文标题】:could not be instantiated com.google.android.youtube.player.YouTubePlayerView 【发布时间】:2016-06-09 07:56:12 【问题描述】:

我是 android 开发的新手。我正在尝试实现 youtube Api。但在我的 XML 文件中,它显示错误“无法实例化: - com.google.android.youtube.player.YouTubePlayerView"

这是我的 MAinActivity 代码:-

public class MainActivity extends YouTubeBaseActivity 
Button b;
private YouTubePlayerView youtubeplayerview;
private YouTubePlayer.OnInitializedListener onInitializedListener;


@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) 
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.activity_main);

    youtubeplayerview=(YouTubePlayerView)findViewById(R.id.Youtube_view);
    onInitializedListener=new YouTubePlayer.OnInitializedListener()
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) 
            youTubePlayer.loadVideo("wvjqFy33HVA");
        

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) 

        
    ;
    b=(Button)findViewById(R.id.button);
    b.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            youtubeplayerview.initialize("API KEY",onInitializedListener);
        
    );


这是我的 XML 文件代码:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
>

<Button
    android:layout_
    android:layout_
    android:text="PLAY VEDIO"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<com.google.android.youtube.player.YouTubePlayerView
    android:layout_
    android:layout_
    android:id="@+id/Youtube_view"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

Here is Error Screenshot

【问题讨论】:

你找到答案了吗? 【参考方案1】:

您是否将 YouTube Android Player API 添加到您的库中?添加解决了我的问题..尝试按照下面给出的步骤添加相同的:

1.从https://developers.google.com/youtube/android/player/downloads/下载YouTube Android Player API jar文件

2.解压 zip 文件,进入 libs -> YouTubeAndroidPlayerApi.jar 并复制它

3.将jar文件粘贴到android studio中的/app/libs/目录

最后将 compile files('libs/YouTubeAndroidPlayerApi.jar') 添加到您的应用级 build.gradle 文件中

同步、构建并检查是否正常

【讨论】:

【参考方案2】:

您是否为 android youtube 播放器生成了 api 密钥? 链接:https://console.developers.google.com

如果是,您必须将这个youtubeplayerview.initialize("API KEY",onInitializedListener);中的“API KEY”更改为您之前生成的api密钥。

无论如何,你可以尝试用这个类替换你的类,YoutubeDeveloperKey 是你生成的密钥:

public class ActivityPlayYouTube extends YouTubeBaseActivity implements
        YouTubePlayer.OnInitializedListener 

    private YouTubePlayer YPlayer;
    private static final String YoutubeDeveloperKey = "YOUR_DEVELOPER_KEY_GOES_HERE";
    private static final int RECOVERY_DIALOG_REQUEST = 1;

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

        YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        youTubeView.initialize(YoutubeDeveloperKey, this);
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.you_tube_api, menu);
        return true;
    

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,
            YouTubeInitializationResult errorReason) 
        if (errorReason.isUserRecoverableError()) 
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
         else 
            String errorMessage = String.format(
                    "There was an error initializing the YouTubePlayer",
                    errorReason.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        
    

    @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(YoutubeDeveloperKey, this);
        
    

    protected YouTubePlayer.Provider getYouTubePlayerProvider() 
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
    

    @Override
    public void onInitializationSuccess(Provider provider,
            YouTubePlayer player, boolean wasRestored) 
        if (!wasRestored) 
            YPlayer = player;
            /*
             * Now that this variable YPlayer is global you can access it
             * throughout the activity, and perform all the player actions like
             * play, pause and seeking to a position by code.
             */
            YPlayer.cueVideo("2zNSgSzhBfM");
        
    

【讨论】:

这个问题发生在布局文件中。我已经在 java 文件中给出了 API 密钥。如何解决布局文件中的问题?

以上是关于无法实例化 com.google.android.youtube.player.YouTubePlayerView的主要内容,如果未能解决你的问题,请参考以下文章

Android:Eclipse:无法实例化以下类:- com.google.android.gms.common.SignInButton

无法解析“com.google.android.gms.gcm.GcmReceiver”?

无法解决导入 com.google.android.gms

com.google.android.gms.maps.MapFragment:无法解析符号“地图”

无法使用 Google Plus 登录,出现 com.google.android.gms.common.api.ApiException: 10:

无法为 com.google.android.gms.tasks.OnSuccessListener 添加依赖项