Blank black box when using YouTubePlayerFragment

EDIT cjohn caught the missing piece. I was forgetting to call init() on my YTPlayerFragment.

New to android dev. I am trying to put a youtube video inside a YouTubePlayerFragment which is nested inside another fragment. Logcat doesn't show any errors, but my player fragment only shows a black box. Placing log statements inside my OnInitializedListener, it seems it's not even being called.

The ContentFragment that contains the YouTubePlayerFragment is a tab inside a ViewPager. My code is below. MainActivity ContentFragment, ContentFragment contains the YouTubePlayerFragment.

MainActivity.java:

public class MainActivity extends FragmentActivity{

    TabsPagerAdapter mAdapter;
    ViewPager mViewPager;
    Boolean mLoggedIn;

    String[] mTabs = {"Library", "Content", "Quotes"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final ActionBar actionBar = getActionBar();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setAdapter(mAdapter);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }
        };

        for (String tab : mTabs) {
            actionBar.addTab(actionBar.newTab().setText(tab).setTabListener(tabListener));
        }


    }

}

TabsPagerAdapter.java:

public class TabsPagerAdapter extends FragmentPagerAdapter{
    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
            case 0:
                return new LibraryFragment();
            case 1:
                return new ContentFragment();
            case 2:
                return new QuotesFragment();
        }

        return null;
    }

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

ContentFragment.java:

public class ContentFragment extends Fragment {

    public ContentFragment() {
        // Required empty public constructor
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setRetainInstance(true);
        View v = inflater.inflate(R.layout.fragment_content, container, false);
        YTPlayerFragment video = YTPlayerFragment.newInstance("nCgQDjiotG0");
        getFragmentManager().beginTransaction().replace(R.id.youtubeFragment, video).commit();
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

}

YTPlayerFragment.java:

public class YTPlayerFragment extends YouTubePlayerSupportFragment {

    private YouTubePlayer activePlayer;
    private final static String DEV_KEY = "KEY";

    public static YTPlayerFragment newInstance(String url) {

        YTPlayerFragment player = new YTPlayerFragment();

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

        player.setArguments(bundle);
        return player;
    }


    private void init() {
        initialize(DEV_KEY, new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                activePlayer = youTubePlayer;
                Log.d("YOUTUBE", "Successfully initialized");
                activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                if (!b) {
                    activePlayer.cueVideo(getArguments().getString("url"));
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                Log.e("YOUTUBE", youTubeInitializationResult.toString());
            }
        });
    }


}

fragment_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="2dp"
              android:clipToPadding="false"
              android:scrollbarStyle="outsideOverlay"
              android:orientation="vertical" >
    <FrameLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/youtubeFragment" />

</LinearLayout>

Answers


Inside your ContentFragment, you're trying to add another Fragment as a child. To do this, you require the child fragment manager, acquired by calling getChildFragmentManager():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setRetainInstance(true);
    View v = inflater.inflate(R.layout.fragment_content, container, false);
    YTPlayerFragment video = YTPlayerFragment.newInstance("nCgQDjiotG0");
    getChildFragmentManager().beginTransaction().replace(R.id.youtubeFragment, video).commit();
    return v;
}

Need Your Help

How to redirect at the route level, not at the action level?

ruby-on-rails ruby-on-rails-3 testing redirect routes

I'd like to redirect campaigns#index to newsletters#index at the route level (config/routes.rb).

Possible to trap write to address (x86 - linux)

c++ c linux x86

I want to be able to detect when a write to memory address occurs -- for example by setting a callback attached to an interrupt. Does anyone know how?