我对“YouTubePlayerSupportFragment”
有疑问我在这里收到错误:
`YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.youtubeplayerview);`
以下是完整代码: 我试图改变一些事情,现在我有一点可理解的错误。 如果我发现它,我会发布solcuón。
更新代码11/06/2015,我收到新的错误:
5429-5429/com.onpocket.activamutua E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)
代码:
public class Activity_demo extends YouTubePlayerSupportFragment /*implements YouTubePlayer.OnInitializedListener */{
private static final int RECOVERY_DIALOG_REQUEST = 10;
public static final String API_KEY = "AIzaSyBHZjt1vrkjuwCp4YqTsADNQVDf6KvAXr0";
private String VIDEO_ID = "c_eHTMhEsHU";
JustifiedTextView tv1;
ConfigActiva cfg;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_own, container, false);
TypefaceUtil.overrideFont(getActivity(), "SERIF", ConfigActiva.nameFontStatic);
ConfigActiva.languageCustom(ConfigActiva.defaultLanguageForce, getActivity());
init(rootView);
return rootView;
}
public void init(View vRoot){
//YouTubePlayerView youTubeView = (YouTubePlayerView) getActivity().findViewById(R.id.youtubeplayerview);
//youTubeView.initialize(API_KEY, this);
YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getChildFragmentManager().findFragmentById(R.id.youtubeplayerview);
//YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.youtubeplayerview);
//youTubePlayerFragment.initialize(API_KEY, this);
youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer.cueVideo(VIDEO_ID);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format("YouTube Error (%1$s)", errorReason.toString());
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
}
}
});
}
/* @Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format("YouTube Error (%1$s)",errorReason.toString());
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}*/
我的XML
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
使用此行Works,但不加载视频:已更新11/06/2015 22:21
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
答案 0 :(得分:0)
您必须动态添加片段,因为:Nested fragments 只需添加一个带id的容器,并在Transaction.replace中使用此id。还可以使用onCreateView进行充气和构建布局。之后,从视图参数中查找onViewCreated(视图视图,...)中的视图,它将使代码更清晰。