0

I would like to play a video in a dialog window, but it doesn't work. The video is instead of the "raw" directory. I don't know where I'm wrong. Can you help me Please? There is the code

ImageButton video_player = (ImageButton) findViewById(R.id.img_btt_on_ear_watch);
        video_player.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.layout_video_player);

                String filePlace = "android.resource://" + getPackageName() + "/" + R.raw.grado_video_cuffie_making;
                VideoView videoV = (VideoView) findViewById(R.id.vv_video_player);
                Uri videoURI = Uri.parse((String) filePlace);
                videoV.setVideoURI(videoURI); //this is the 128 raw
                videoV.setMediaController(new MediaController(context));
                videoV.start();

                ImageButton dialogButton = (ImageButton) dialog.findViewById(R.id.img_btt_vv_close);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();

            }
        });

There is the Logcat

11-15 10:40:50.666 3893-3893/com.huawei.headphones.huaweimusic E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.huawei.headphones.huaweimusic, PID: 3893
                                                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(android.net.Uri)' on a null object reference
                                                                                     at com.huawei.headphones.huaweimusic.Activity_Cuffia1$1.onClick(Activity_Cuffia1.java:128)
                                                                                     at android.view.View.performClick(View.java:5610)
                                                                                     at android.view.View$PerformClick.run(View.java:22265)
                                                                                     at android.os.Handler.handleCallback(Handler.java:751)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Twing90
  • 407
  • 2
  • 5
  • 15
  • This error says that the `videoV` variable is null, you therefore get a NullPointerException. Check that in `layout_video_player.xml`, you have a VideoView with the ID `vv_video_player` : if not, that would cause the `findViewById` function to return null – MeanStreet Nov 15 '17 at 09:54
  • the VideoView ID is correct and I have it in the layout_video_player.xml – Twing90 Nov 15 '17 at 09:56
  • This will search in the IDs of the ImageButton actually, try `dialog.findViewById(...)` instead – MeanStreet Nov 15 '17 at 10:00

1 Answers1

1

Use this

 VideoView videoV = (VideoView) dialog.findViewById(R.id.vv_video_player);

insted of this

VideoView videoV = (VideoView) findViewById(R.id.vv_video_player);
Goku
  • 9,102
  • 8
  • 50
  • 81