由于某些原因,即使我正在使用SupportFragment Activity ,AppCompatActivity也拒绝让我嵌入Youtube视频。每当 Activity 包含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崩溃了该应用程序吗?

最佳答案

由于YouTubeBaseActivity不具备AppCompatActivity的功能,出于某种原因,我认为它没有扩展“YouTubeBaseActivity”。也许有较新的版本。但我会说那就是问题。

关于android - AppCompatActivity因嵌入式YouTube视频而崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37098897/

10-11 13:32