android - getSurfaceTexture() 返回 null

标签 android

我有以下代码:

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"TextureViewpreview_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/

相关文章:

android - 在 Android 中以编程方式从模拟器中获取 SDCARD 的文件路径是什么?

java - 如何使用另一个类中的 int?

android - 在 Android 中伪造应用程序崩溃

android - 使用 Google Assistant 添加语音控制

android - 无法在 Android 应用程序中使用 Google Maps API

android - Eclipse-创建新 Activity

java - Mapbox OfflineRegionObserver 监听器注销

android - Gradle 'AppName'项目刷新失败

java - 如何在屏幕上绘制文本字段

java - 设置菜单类时出错