Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getSurfaceTexture() returning null

Tags:

android

I have the following code:

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

The layout buts.xml contains a set of buttons as well as a TextureView labelled "@id/tex1". It appears that preview_texture_view.getSurfaceTexture() is returning null. The documentation says:

This method may return null if the view is not attached to a window or if the surface texture has not been initialized yet.

I assume the surface must already be attached to a window because the buttons are visible, so I guess my mistake must be not having initialized preview_texture_view - but I have no idea what it means to "initialize" preview_texture_view other than what I have already done. I have seen some related sample code and can not figure out any initialization process.

EDIT: Maybe it takes a little time to attach to the window or the be initialized. Maybe I need to set up a callback for when its ready?.. just guessing.

like image 461
Mick Avatar asked Feb 12 '14 17:02

Mick


1 Answers

Solved: My guess turned out to be correct. To fix, set up the following:

Make activity implement

TextureView.SurfaceTextureListener 

add method onSurfaceTextureAvailable()

call

preview_texture_view.setSurfaceTextureListener(this);
like image 50
Mick Avatar answered Nov 15 '22 10:11

Mick