0

After following some tutorials I made an app that shows Youtube Videos of a specific Youtube playlist in listview.

How can I parse the corresponding video Id to play the video in my app itself using a VideoView

My PlaylistCardAdapter

    //  click listener to play the video
    holder.mThumbnailImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), Activity2.class);
            intent.putExtra("VidId", video.getId());
            view.getContext().startActivity(intent);

        }
    });

My second Activity in which I want to parse the VideoId

 public class Activity2 extends YouTubeBaseActivity implements View.OnClickListener {
Button mbutton;
private YouTubePlayerView myouTubePlayerView;
private YouTubePlayer.OnInitializedListener monInitializedListener;

  String VidKey = getIntent().getExtras().getString("VidId");//error here as per logcat

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player);myouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view);
    monInitializedListener = new YouTubePlayer.OnInitializedListener() {

  @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.loadVideo(VidKey);
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }
    };

    mbutton = (Button) findViewById(R.id.button_play_youtube_video);
    mbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myouTubePlayerView.initialize(YOUTUBE_API_KEY, monInitializedListener);
        }
    });
}

Error I'm getting

  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.paperwrrk.playlistyoutube/com.paperwrrk.playlistyoutube.Activity2}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2361)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                                               at com.paperwrrk.playlistyoutube.Activity2.<init>(Activity2.java:19)
                                                                               at java.lang.Class.newInstance(Native Method)
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 
Gbenga A
  • 209
  • 2
  • 4
  • 11

1 Answers1

0

To play YouTube videos within your app you have integrate

YouTube Android Player API

in your project.

It will provide a custom view that you will add in your xml like this.

<com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtube_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="30dp" />

And for further implementation help you can follow this tutorial.

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84