AppCompatActivity 因嵌入的 YouTube 视频而崩溃 [英] AppCompatActivity crashing with embedded YouTube videos

查看:23
本文介绍了AppCompatActivity 因嵌入的 YouTube 视频而崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,即使我正在使用 SupportFragment 活动,AppCompatActivity 也拒绝让我嵌入 Youtube 视频.每次活动包含 Youtube 片段时,应用程序都会崩溃.我该如何解决这个问题?

For some reason, AppCompatActivity refuses to let me embed Youtube videos even though I am using the SupportFragment activity. The app crashes every time an activity contains a Youtube fragment. How can I fix this?

public class Insane1x1 extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {

    public static final String API_KEY = "AIzaSyBqaaaaaaa";
    public static final String VIDEO_ID = "3MvnRsItEmg"; //1-1

    //private YouTubePlayer youTubePlayer;
    private YouTubePlayerSupportFragment youTubePlayerFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insane1x1);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide(); 

          youTubePlayerFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.youtubeplayerfragment);
            youTubePlayerFragment.initialize(API_KEY, 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.insane1x1, menu);
        return true;
    }

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onInitializationFailure(Provider provider,
            YouTubeInitializationResult result) {
        Toast.makeText(getApplicationContext(), 
                "YouTubePlayer.onInitializationFailure()", 
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player,
            boolean wasRestored) {

        //youTubePlayer = player;

        Toast.makeText(getApplicationContext(), 
                "YouTubePlayer.onInitializationSuccess()", 
                Toast.LENGTH_LONG).show();

        if (!wasRestored) {
              player.cueVideo(VIDEO_ID);
            }

    }

这里是 xml 文件:

And here's the xml file:

 <fragment
    android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:id="@+id/youtubeplayerfragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

是否是 YouTubePlayer 在 onInitializationSuccess 方法中提示视频时导致应用崩溃?

Is it the YouTubePlayer that is crashing the app, when it is cuing the video in the onInitializationSuccess method??

推荐答案

我认为这是因为它没有扩展YouTubeBaseActivity",出于某种原因,尽管 YouTubeBaseActivity 没有 AppCompatActivity 那么多的功能.也许有更新的版本.但我想说这就是问题所在.

I think it's cz its not extending "YouTubeBaseActivity", for some reason though YouTubeBaseActivity doesn't have as much functionality as AppCompatActivity. Maybe there are newer versions. But I'dd say thats the problem.

这篇关于AppCompatActivity 因嵌入的 YouTube 视频而崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆