嵌入的YouTube视频导致AppCompatActivity崩溃

时间:2016-05-08 10:41:07

标签: android youtube

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

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文件:

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

在使用onInitializationSuccess方法修改视频时,YouTubePlayer是否会导致应用崩溃?

1 个答案:

答案 0 :(得分:0)

我认为,由于某些原因,它没有扩展“YouTubeBaseActivity”,尽管YouTubeBaseActivity没有像AppCompatActivity那样多的功能。也许有更新的版本。但我会说这就是问题所在。