我正在尝试使用YouTubePlayerSupportFragment将youtube视频整合到Fragment中。我使用了youtubeandroidplayerapi。我在logcat中遇到以下错误 -
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.pepup.league.ui.fragments.VideosFragment.onCreateView(VideosFragment.java:109)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:15264)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15264)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:704)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15264)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2161)
at android.view.View.measure(View.java:15264)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2129)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1240)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1433)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4607)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
at android.view.Choreographer.doCallbacks(Choreographer.java:567)
at android.view.Choreographer.doFrame(Choreographer.java:536)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
错误显示在行 -
youTubePlayerSupportFragment.initialize(YoutubeDeveloperKey,new OnInitializedListener()
这是我的代码 -
VideosFragment.java
public class VideosFragment extends YouTubePlayerSupportFragment{
private FragmentActivity myContext;
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "xyz";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
public void onAttach(Activity activity) {
if (activity instanceof FragmentActivity) {
myContext = (FragmentActivity) activity;
}
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.videos, container, false);
rootView.setVerticalScrollBarEnabled(false);
YouTubePlayerSupportFragment youTubePlayerSupportFragment = (YouTubePlayerSupportFragment) getFragmentManager().findFragmentById(R.id.youtube_view);
youTubePlayerSupportFragment.initialize(YoutubeDeveloperKey,new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
// TODO Auto-generated method stub
if (!wasRestored) {
YPlayer = player;
YPlayer.cueVideo("2zNSgSzhBfM");
}
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult errorReason) {
// TODO Auto-generated method stub
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(myContext, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(myContext, errorMessage, Toast.LENGTH_LONG).show();
}
}
});
return rootView;
}
@Override
public void initialize(String s, OnInitializedListener onInitializedListener) {
}
}
videos.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<RelativeLayout
android:id="@+id/layout4"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="62dp"
android:layout_marginRight="32dp"
android:cropToPadding="true"
android:padding="1dp"
android:scaleType="centerCrop" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:0)
想出了这个问题。如果我使用getChildFragmentManager()
方法,那么它可以工作。
YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment)getChildFragmentManager().
findFragmentById(R.id.add_edit_youtube_youtube_fragment);
答案 1 :(得分:0)
我也有这个问题。在 Vikas Gupta 的提示旁边,使用getChildFragmentManager()
,我还必须将与该片段相关的代码移到onViewCreated()
:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
youTubePlayerFragment = (YouTubePlayerSupportFragment) getChildFragmentManager()
.findFragmentById(R.id.youtube_player_fragment);
youTubePlayerFragment.initialize(BuildConfig.API_KEY,
new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean b) {
// code when initialization is successful
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
Timber.e("Youtube Player could not be initialized.");
//handle error
}
});
}
看起来片段在视图仍在创建时返回null。因此,在调用和处理youtube片段之前,我必须等待完成。