我有以下代码:
LayoutInflater factory = LayoutInflater.from(this);
View view_with_buttons_on = factory.inflate(R.layout.buts, null);
view_with_buttons_on.setBackgroundColor(Color.TRANSPARENT);
game_frame_layout = new FrameLayout(getApplicationContext());
surface_view_extension = new SurfaceViewExtension(getApplicationContext());
game_frame_layout.addView(surface_view_extension);
game_frame_layout.addView(view_with_buttons_on);
setContentView(game_frame_layout);
preview_texture_view = (TextureView) findViewById(R.id.tex1);
try {
camera.setPreviewTexture(preview_texture_view.getSurfaceTexture());
} catch (IOException e) {
e.printStackTrace();
}
布局 buts.xml 包含一组按钮以及标记为 "@id/tex1"
的 TextureView
。
preview_texture_view.getSurfaceTexture()
似乎返回 null。文档说:
This method may return null if the view is not attached to a window or if the surface texture has not been initialized yet.
我假设表面必须已经连接到窗口,因为按钮是可见的,所以我想我的错误一定是没有初始化 preview_texture_view - 但我不知道“初始化” preview_texture_view 是什么意思,除了我所拥有的已经完成了。我看过一些相关的示例代码,无法弄清楚任何初始化过程。
编辑: 附加到窗口或初始化可能需要一些时间。也许我需要在准备就绪时设置回调?..只是猜测。
最佳答案
已解决:事实证明我的猜测是正确的。要修复,请设置以下内容:
使 Activity 实现
TextureView.SurfaceTextureListener
添加方法onSurfaceTextureAvailable()
打电话
preview_texture_view.setSurfaceTextureListener(this);
关于android - getSurfaceTexture() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21735456/