我有一个MenuActivity
,MenuActivity
我有一个SeguridadMenuFrament,而SeguridadMenuFrament
我有SeguridadFragment。
在SeguridadFrament
我需要实施YoutubePlayer
。
在我的fragment_seguridad.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/TR_CARACTERISTICAS"
android:textSize="@dimen/text_14"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/tvCaracteristicas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text_12"
app:layout_constraintTop_toBottomOf="@id/tvTitle"
android:layout_marginTop="20dp"/>
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtubePF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/tvCaracteristicas"/>
</android.support.constraint.ConstraintLayout>
在SeguridadFragment.java
中public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_seguridad, container, false);
mYoutubePlayerFragment = new YouTubePlayerSupportFragment();
mYoutubePlayerFragment.initialize(YoutubeDeveloperKey, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
youTubePlayer.cueVideo("NMwE93GQcKs");
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.youtubePF, mYoutubePlayerFragment);
fragmentTransaction.commit();
return rootView;
}
但是当运行应用程序并显示fragment
时,应用程序崩溃并显示:
12-04 10:12:56.878 4342-4342 / com.pixelcom.beta.pixelkartingsocios E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.pixelcom.beta.pixelkartingsocios,PID:4342 java.lang.IllegalArgumentException:Service Intent必须是显式的:Intent {act = com.google.android.youtube.api.service.START} 在android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1373) 在android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1482) 在android.app.ContextImpl.bindService(ContextImpl.java:1450) 在android.content.ContextWrapper.bindService(ContextWrapper.java:636) 在com.google.android.youtube.player.internal.r.e(未知来源) 在com.google.android.youtube.player.YouTubePlayerView.a(未知来源) 在com.google.android.youtube.player.YouTubePlayerSupportFragment.a(未知来源) 在com.google.android.youtube.player.YouTubePlayerSupportFragment.onCreateView(未知来源) 在android.support.v4.app.Fragment.performCreateView(Fragment.java:2354) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) 在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) 在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) 在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) 在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) 在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229) 在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:700) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)*
答案 0 :(得分:3)
您需要在布局中使用FrameLayout而不是Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/TR_CARACTERISTICAS"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/tvCaracteristicas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text_12"
app:layout_constraintTop_toBottomOf="@id/tvTitle"
android:layout_marginTop="20dp"/>
<FrameLayout
android:id="@+id/frame_fragment"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</LinearLayout>
然后在你的Fragment类中使用这段代码:
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_fragment, youTubePlayerFragment);
transaction.commit();
youTubePlayerFragment.initialize(YoutubeDeveloperKey, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
YPlayer = youTubePlayer;
YPlayer.setFullscreen(false);
YPlayer.loadVideo("NMwE93GQcKs");
YPlayer.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
希望它有所帮助!