我试图在片段类中调用构造函数。但是它显示错误。
这是构造函数代码:
public CameraPreview(Context fragment, Camera camera,
PreviewCallback previewCb,
AutoFocusCallback autoFocusCb) {
super(fragment);
mCamera = camera;
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
然后我尝试访问另一个片段类:
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout) getView().findViewById(R.id.cameraPreview);
preview.addView(mPreview);
我注意到问题是"这个"。我该如何解决?谢谢。
答案 0 :(得分:1)
如果此调用是片段,则应为:
mPreview = new CameraPreview(this.getActivity(), mCamera, previewCb, autoFocusCB);
this.getActivity()
将为您提供上下文,Fragment
不会扩展Context类,因此这实际上是错误的命名:Context fragment
。