我创建了一个名为 CaptureView
的自定义 SurfaceView
并尝试将其添加到 main.xml
文件中:
<dev.recorder.client.CaptureView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/capturePreview"/>
该应用程序似乎工作正常,但如果我在 Eclipse 中从 main.xml
选项卡切换到布局,则会出现文本 NullPointerException
而不是布局预览。
在 Activity 中,我按以下方式绑定(bind)控件:
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);
}
有谁知道如何解决这个问题?
最佳答案
确保您在 onFinishInflate 中而不是在构造函数中初始化 View 。 布局预览代码可能会通过不同的代码路径初始化您的控件。
关于android - 使用自定义 View 或 SurfaceView 时如何避免 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/985851/