javaandroidandroid-dialog

NullPointerException: findViewById returns null in Dialog


I´m trying to show a video in a dialog but the VideoView can´t be found, although the id is correct. I get a NullPointerException in this line:

VideoView vv = (VideoView) findViewById(R.id.videoview1);

This is my code:

                Dialog dialog2 = new Dialog(getContext(),
                    android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
            dialog2.setContentView(R.layout.video);

            VideoView vv = (VideoView) findViewById(R.id.videoview1);
            String uri = "android.resource://" + theGameActivity.getPackageName() +"/"+R.raw.video6;
dialog2.show();
            vv.start();

Solution

  • Try this..

    Change this..

    VideoView vv = (VideoView) findViewById(R.id.videoview1);
    

    to

    VideoView vv = (VideoView) dialog2.findViewById(R.id.videoview1);
    

    Because View must refer Dialog