使用YouTubePlayerSupportFragment崩溃 [英] crash using YouTubePlayerSupportFragment
问题描述
我有一个从片段开始的活动
I have an activity that extends from a fragment
public class Youtube_visor extends Fragment{ ...
以及使用YouTubePlayerSupportFragment创建新的YouTube播放器的方法
and a method that creates a new YouTube player using YouTubePlayerSupportFragment
这是我的代码:
public void playVideo () {
fragmentManager = getActivity().getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();
fragmentTransaction.add(R.id.fragmentz, player); // fragmentz es un linearlayout que hay en el xml
fragmentTransaction.commit();
player.initialize(Static_generals.youtubeKey,new OnInitializedListener() {
public void onInitializationSuccess(Provider arg0,
YouTubePlayer arg1, boolean arg2) {
if (!arg2) {
arg1.cueVideo("3OhGkg_XT3o");
}
}
@Override
public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
// Error
}
});
}
这在平板电脑上完美运行,我有一个包含两个片段的活动,左边是视频列表,右边是创建YouTube播放器的片段
This works perfectly on the tablet, I have an activity with two fragments, left a list of videos, and right, the fragment which create the YouTube player
我的问题是我不想创建一个新的YouTubePlayer,我想在xml中声明一个并始终使用它. 这是在xml中声明的片段:
My problem is I do not want to create a new YouTubePlayer, I want to declare one in the xml and always use that. This is the fragment declared in the xml:
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
现在,在我的代码中,我替换以下行:
Now, in my code, I replace this line:
YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();
并添加以下新行:
YouTubePlayerSupportFragment player = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);
但总是崩溃. 我搜索了有关此信息,但没有太多可用信息,或者找不到它:)
but always crash. I searched for information about it but there is not much available, or have not found it :)
我在做什么错了?
我猜我在说我的片段是错误的,因为当我做一个新的片段时可以正常工作
I guess I'm calling my fragment wrong, because when I do a new one working properly
感谢您的帮助.
感谢和亲切问候
推荐答案
看起来您已经在XML中放入了YouTubePlayerFragment
,但是您试图将其转换为代码中的YouTubePlayerSupportFragment
.尝试替换此行:
Looks like you've put a YouTubePlayerFragment
in your XML, but you're trying to cast it to a YouTubePlayerSupportFragment
in the code. Try replacing this line:
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
与此:
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
这篇关于使用YouTubePlayerSupportFragment崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!