How to avoid NullPointerException when using custom Views or SurfaceViews
-
13-09-2019 - |
Question
I created a custom SurfaceView
called CaptureView
and tried to add it into main.xml
file:
<dev.recorder.client.CaptureView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/capturePreview"/>
The application seems to work fine but if I switch from main.xml
tab to Layout in Eclipse the text NullPointerException
appears instead of layout preview.
In the Activity I binded the controls the following way:
setContentView(R.layout.main);
bindControls();
private void bindControls()
{
videoPreview = (CaptureView)findViewById(R.id.capturePreview);
txtstatus = (TextView)findViewById(R.id.txtMode);
txtTimer = (TextView)findViewById(R.id.txtTime);
}
Does anyone know how this issue could be solved?
Solution
make sure that you are initializing the view in onFinishInflate and not in the constructor. the layout preview code might initialize your control through a different code path.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow