无法从对话框中检索VideoView

时间:2015-05-05 18:43:11

标签: android android-videoview

我有一个自定义对话框(playvideo.xml),里面有一个视频。

我使用以下代码,但在videoView.setVideoURI(video);

获取空指针异常
final Dialog dialog = new Dialog(this,R.style.FullHeightDialog);
    dialog.setContentView(R.layout.playvideo);


    VideoView videoView = (VideoView)findViewById(R.id.videoView);
    Uri video = Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw.howtoplaymancalagp);

    //if uncommented, then gives null pointer exception
    //videoView.setVideoURI(video);

     dialog.show();
    //videoView.start();

我无法弄清楚为什么videoView为空。

1 个答案:

答案 0 :(得分:3)

您正在调用此活动findViewById(),并且找不到视频视图。

在此行中,您可以设置对话框的内容视图:

dialog.setContentView(R.layout.playvideo);

如果您想获取videoView,则需要在该视图上调用findViewById。试试这个:

VideoView videoView = (VideoView) dialog.getView().findViewbyId(R.id.videoView);