我是新的Android开发,我一直有,我一直没能解决的问题。我主要是用code从SDK提供的例子,让我不知道这里发生了什么。我只是想创建一个自定义视图GhostSurfaceCameraView延伸SurfaceView。这是我的类定义文件GhostSurfaceCameraView.java:
I'm new to Android development and I've been having an issue that I haven't been able to fix. I'm mostly using code from examples provided in the SDK so I'm not sure what's happening here. I'm simply trying to create a custom view GhostSurfaceCameraView that extends SurfaceView. Here's my class definition file GhostSurfaceCameraView.java:
public class GhostSurfaceCameraView extends SurfaceView implements
SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
GhostSurfaceCameraView(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
parameters.set("orientation", "portrait");
// parameters.setRotation(90); // API 5+
mCamera.setParameters(parameters);
mCamera.startPreview();
}}
这是我ghostviewscreen.xml:
And this is in my ghostviewscreen.xml:
<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
现在的我做了活动:
protected void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ghostviewscreen);
}
当的setContentView被调用,将引发异常:
When setContentView gets called, an exception is thrown:
二进制XML文件09-17 22:47:01.958:错误/ ERROR(337): 错误在code: android.view.InflateException:二进制 XML文件行#14:错误充气 类 com.alpenglow.androcap.GhostSurfaceCameraView
Binary XML file 09-17 22:47:01.958: ERROR/ERROR(337): ERROR IN CODE: android.view.InflateException: Binary XML file line #14: Error inflating class com.alpenglow.androcap.GhostSurfaceCameraView
谁能告诉我,为什么我得到这个错误?谢谢
Can anyone tell me why I get this error? Thanks
我想我想通了,为什么这是行不通的。我只是提供了一个构造一个参数语境的情况时,我应该已经提供了一个构造函数的两个参数语境下,AttributeSet中的案例。我还需要给构造函数(S)公共访问。这是我的修正:
I think I figured out why this wasn't working. I was only providing a constructor for the case of one parameter 'context' when I should have provided a constructor for the two parameter 'Context, AttributeSet' case. I also needed to give the constructor(s) public access. Here's my fix:
public class GhostSurfaceCameraView extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
public GhostSurfaceCameraView(Context context)
{
super(context);
init();
}
public GhostSurfaceCameraView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public GhostSurfaceCameraView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}