title: Youtube Android API NullPointerException Error in Youtube Fragment
body: <p>Basically i'm trying to create youtube fragment. This fragment displays when any List item is selected.
  I'm getting NullPointerExpection error. Not exactly sure, what am i doing wrong. </p>
  
  <p>Youtube fragment</p>
  
  <pre><code>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.YouTubePlayer.Provider;
  import com.google.android.youtube.player.YouTubePlayerFragment;
  import android.app.Fragment;
  import android.app.FragmentManager;
  import android.app.FragmentTransaction;
  import android.content.Context;
  
  import com.google.android.youtube.player.YouTubePlayerView;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Toast;
  
  
  public class Youtube extends Fragment implements OnInitializedListener{
       public static final String API_KEY = "MyKey";
       public static final String VIDEO_ID = "o7VVHhK9zf0";
  
       /*public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
          setContentView(R.layout.youtube_fragment);
          YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getActivity().getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);
             youTubePlayerFragment.initialize(API_KEY, this);
          }
  */
   @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
       YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getActivity().getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);
         youTubePlayerFragment.initialize(API_KEY, this);
       return inflater.inflate(R.layout.youtube_fragment, container, false);
          }
  
          private void setContentView(int youtubeFragment) {
              //YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getActivity().getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);
               //  youTubePlayerFragment.initialize(API_KEY, this);
                 /*Fragment fragment = new YouTubePlayerFragment();
                 FragmentManager fm = getActivity().getFragmentManager();
                 FragmentTransaction ft = fm.beginTransaction();
                 ft.replace(R.id.youtubeplayerfragment, fragment);
                 ft.addToBackStack(null);
                 ft.commit(); 
  
                 YouTubePlayerFragment youtubeFrag = (YouTubePlayerFragment) fragment;
                 youtubeFrag.initialize(API_KEY, this);*/
          }
  
          @Override
           public void onInitializationFailure(Provider provider,
             YouTubeInitializationResult result) {
            Toast.makeText(getApplicationContext(), 
              "onInitializationFailure()", 
              Toast.LENGTH_LONG).show();
           }
  
          private Context getApplicationContext() {
              // TODO Auto-generated method stub
              return null;
          }
  
          @Override
          public void onInitializationSuccess(Provider provider, YouTubePlayer player,
                  boolean wasRestored) {
                if (!wasRestored) {
                      player.cueVideo(VIDEO_ID);
                    }
          } 
  </code></pre>
  
  <p>youtube_fragments.xml</p>
  
  <pre><code>&lt;LinearLayout 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"
      android:orientation="vertical"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".Youtube" &gt;
  
      &lt;fragment
          android:id="@+id/youtubeplayerfragment"
          android:layout_width="match_parent"
          android:layout_weight="0.02"
          class="com.google.android.youtube.player.YouTubePlayerFragment" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Errors</p>
  
  <pre><code>03-18 14:57:36.470: E/AndroidRuntime(1339): FATAL EXCEPTION: main
  03-18 14:57:36.470: E/AndroidRuntime(1339): Process: com.example.actionbartab, PID: 1339
  03-18 14:57:36.470: E/AndroidRuntime(1339): java.lang.NullPointerException
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at com.example.actionbartab.Youtube.onCreateView(Youtube.java:36)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.app.Fragment.performCreateView(Fragment.java:1700)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.os.Handler.handleCallback(Handler.java:733)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.os.Handler.dispatchMessage(Handler.java:95)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.os.Looper.loop(Looper.java:136)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at android.app.ActivityThread.main(ActivityThread.java:5017)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at java.lang.reflect.Method.invokeNative(Native Method)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at java.lang.reflect.Method.invoke(Method.java:515)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  03-18 14:57:36.470: E/AndroidRuntime(1339):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>