扩展类时出错错误

ecc*_*ped 188 java xml android class surfaceview

我正在尝试创建一个GhostSurfaceCameraView扩展的自定义视图SurfaceView.这是我的类定义文件

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是在我的ghostviewscreen.xml中:

<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

现在我做的活动:

protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ghostviewscreen);
    }
}
Run Code Online (Sandbox Code Playgroud)

setContentView()被调用时,抛出一个异常:

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
Run Code Online (Sandbox Code Playgroud)

谁能告诉我为什么会出现这个错误?谢谢.

ecc*_*ped 367

我想我弄清楚为什么这不起作用.当我应该为两个参数'Context,AttributeSet'的情况提供构造函数时,我只提供了一个参数'context'的构造函数.我还需要为构造函数提供公共访问权限.这是我的修复:

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();
        }
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!!在示例中没有任何地方可以找到有关重载所有构造函数的必要性!你节省了我几小时(几天?)的时间. (5认同)
  • 有时最简单的事情可能是一个问题:)很高兴知道这两个参数都用于充气. (4认同)
  • 哦,应该早点见过!消息"View不使用2-**OR**3参数View构造函数"有点误导. (2认同)

KVN*_*Nam 45

@Tim - 两个构造函数都不是必需的,只需要ViewClassName(Context context, AttributeSet attrs )构造函数.经过几个小时的浪费时间后,我发现了这种痛苦的方式.

我对Android开发很新,但我在这里做了一个疯狂的猜测,可能是因为我们View在XML文件中添加自定义类,我们在XML中设置了几个属性,这需要在实例化时进行处理.比我知识渊博的人能够更清楚地了解这件事.


rmt*_*eis 19

"错误膨胀类"消息的另一个可能原因可能是错误拼写在XML中指定的完整包名称:

<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

在Eclipse XML编辑器中打开布局XML文件应突出显示此问题.

  • 这确实是我的应用程序的修复程序.com.zerokol.views.joystickview成为com.zerokol.views.JoystickView而且它有效! (2认同)