android NullPointerException:findViewById在对话框中返回null

xe55xuns  于 2023-04-28  发布在  Android
关注(0)|答案(3)|浏览(82)

我试图在对话框中显示视频,但找不到VideoView,尽管id是正确的。我在这一行得到一个NullPointerException:

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

这是我的代码:

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();
mu0hgdu0

mu0hgdu01#

试试这个。
换这个。

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

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

因为View必须引用Dialog

unhi4e5o

unhi4e5o2#

变更

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

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

因为videoview1R.layout.video的一部分,而dialog2视图布局是R.layout.video

1sbrub3j

1sbrub3j3#

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

你需要使用对话框对象.指定符来获取视频视图id

相关问题