YouTubePlayerSupportFragment

YouTubePlayerSupportFragment

我正在为 viewpager 使用适配器来显示 youtube 视频。

public class ViewPagerYoutubeAdapter  extends FragmentStatePagerAdapter {

String whichlist, site;
private int totalItemsSize;// = 18;

public ViewPagerYoutubeAdapter(FragmentManager supportFragmentManager,
        String whichlist, String site, Context context) {
    super(supportFragmentManager);
    this.whichlist = whichlist;
    this.site = site;
    if (whichlist.equalsIgnoreCase("geetaMessages")) {
        totalItemsSize = context.getResources().getStringArray(
                R.array.geeta_messages_name).length;
    }
}


@Override
public Fragment getItem(int position) {





return ArrayListYoutubeFragment.newInstance(position, whichlist, site);
}

@Override
public int getCount() {
    return totalItemsSize;
}
}

和 ArrayListYoutubeFragment 如下:暂时忽略参数。我现在只想在所有页面上播放一个视频。
public class ArrayListYoutubeFragment extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener{

private String currentVideoID = "video_id";
private YouTubePlayer activePlayer;

public ArrayListYoutubeFragment() {
    // TODO Auto-generated constructor stub

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragments_demo, container, false);

    YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getFragmentManager()
            .findFragmentById(R.id.youtube_fragment);
    youTubePlayerFragment.initialize(Constants.DEVELOPER_KEY, this);
    return v;
}

@SuppressLint("NewApi")
public static ArrayListYoutubeFragment newInstance(int position,
        String whichlist, String site) {

    ArrayListYoutubeFragment playerYouTubeFrag = new ArrayListYoutubeFragment();

    Bundle bundle = new Bundle();
    bundle.putString("url", whichlist);

    playerYouTubeFrag.setArguments(bundle);

    return playerYouTubeFrag;
}


// @Override
public void onYouTubeVideoPaused() {
    activePlayer.pause();
}


@Override
public void onInitializationFailure(Provider arg0,
        YouTubeInitializationResult arg1) {
    // TODO Auto-generated method stub

}


@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
        boolean wasRestored) {
    // TODO Auto-generated method stub
     if (!wasRestored) {
          player.cueVideo("nCgQDjiotG0");
        }
}
}

但我得到低于错误。
12:36:44.422: E/AndroidRuntime(8625): FATAL EXCEPTION: main
12:36:44.422: E/AndroidRuntime(8625): Process:       com.example.actionbarsherlock, PID: 8625
12:36:44.422: E/AndroidRuntime(8625): java.lang.NullPointerException
12:36:44.422: E/AndroidRuntime(8625):   at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)
12:36:44.422: E/AndroidRuntime(8625):   at com.example.viewpager.ArrayListYoutubeFragment.onStart(ArrayListYoutubeFragment.java:31)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.Fragment.performStart(Fragment.java:1524)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-25 12:36:44.422: E/AndroidRuntime(8625):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
12:36:44.422: E/AndroidRuntime(8625):   at android.view.View.measure(View.java:16497)
12:36:44.422: E/AndroidRuntime(8625):   at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
12:36:44.422: E/AndroidRuntime(8625):   at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12:36:44.422: E/AndroidRuntime(8625):   at android.view.View.measure(View.java:16497)
12:36:44.422: E/AndroidRuntime(8625):   at

最佳答案

遇到同样的问题,多次搜索stackoverflow。
解决方案只是简单地扩展 Fragment 而不是 YouTubePlayerSupportFragment

public class FragmentTrailers extends Fragment implements YouTubePlayer.OnInitializedListener {
    public static final String DEVELOPER_KEY = BuildConfig.YOUTUBE_API;
    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private String url;

    YouTubePlayerSupportFragment youTubePlayerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_fragment_trailers, container, false);

        youTubePlayerView = YouTubePlayerSupportFragment.newInstance();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.youtube_fragment, youTubePlayerView).commit();

        youTubePlayerView.initialize(DEVELOPER_KEY, this);

        return view;
    }
}

XML 布局文件。
      <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/youtube_fragment"/>

关于android - com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart 处的 NullPointerException(来源不明),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23301662/

10-09 18:58