I had the same problems - I wanted to add a YouTube player to my application, but albo I did not want to remove Sherlock from it (based on the support library). And what's bad, I could not use any of the players because I had errors (inflating a fragment, launching YouTubePlayerView without much action, etc.).
What worked: I used SherlockFragmentActivity, FragmentManager (getSupportFragmentManager ()) and YouTubePlayerSupportFragment. Instead of adding it to XML, I created everything from the code. My layout is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:id="@+id/fragmentz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" />
and Java code:
package com.example.youtubetesting; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubePlayer; import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener; import com.google.android.youtube.player.YouTubePlayerSupportFragment; import com.google.android.youtube.player.YouTubePlayer.Provider; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; public class MainActivity extends SherlockFragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); YouTubePlayerSupportFragment fragment = new YouTubePlayerSupportFragment(); fragmentTransaction.add(R.id.fragmentz, fragment); fragmentTransaction.commit(); fragment.initialize("Your API KEY HERE", new OnInitializedListener() { @Override public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1, boolean arg2) { if (!arg2) { arg1.loadVideo("wKJ9KzGQq0w"); } } @Override public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { } }); } }
I donβt know why Android was returning errors when I inflated my views in the usual way, but this works fine.
source share