【问题标题】:AppCompatActivity crashing with embedded YouTube videosAppCompatActivity 与嵌入的 YouTube 视频一起崩溃
【发布时间】:2016-05-08 10:41:06
【问题描述】:

由于某种原因,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"/>

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

【问题讨论】:

  • 我注意到互联网上有很多不同的 Youtube API Jar 文件。我目前正在使用 developer.google 网站上的最新版本。
  • 我建议包含一个 logcat,以便潜在的响应者更容易找出您的问题
  • @FaizMalkani 该应用不会在我的 Bluestacks 模拟器上崩溃,但它会在手机上崩溃。
  • “应用程序在我的 Bluestacks 模拟器上没有崩溃,但它在手机上崩溃” - 在手机上调试时 LogCat 输出是什么?
  • 我刚刚使用与您的代码非常相似的代码组合了一个sample project,并且在外部设备上一切正常

标签: android youtube


【解决方案1】:

我认为这是因为它没有扩展“YouTubeBaseActivity”,尽管 YouTubeBaseActivity 没有 AppCompatActivity 那么多的功能。也许有更新的版本。但我想说这就是问题所在。

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 2019-07-14
    • 2023-03-26
    • 2014-06-24
    • 2013-10-19
    • 2021-12-04
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多