188

我正在尝试创建一个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();
    }
}

这是在我的 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()被调用时,抛出异常:

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

谁能告诉我为什么会出现此错误?谢谢。

4

10 回答 10

372

我想我明白了为什么这不起作用。当我应该为两个参数 '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();
        }
于 2010-09-18T00:14:13.437 回答
45

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

我对 Android 开发非常陌生,但我在这里做一个疯狂的猜测,这可能是由于我们View在 XML 文件中添加自定义类,我们在 XML 中为其设置了几个属性,这需要在实例化时进行处理。不过,比我知识渊博的人将能够更清楚地阐明这件事。

于 2013-04-15T14:19:12.777 回答
19

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

<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

在 Eclipse XML 编辑器中打开布局 XML 文件应该会突出显示这个问题。

于 2014-11-17T01:00:51.327 回答
2

在 xml 中编写完整的类路径很重要。当只写入子类的名称时,我得到“错误膨胀类”。

于 2017-01-25T19:37:53.833 回答
1

在过去的几个小时里,这个错误一直困扰着我。事实证明,我已经将自定义视图库作为模块添加到 Android Studio 中,但我忽略了将它作为依赖项添加到应用程序的build.gradle.

dependencies {
    ...
    compile project(':gifview')
}
于 2016-01-14T05:28:27.407 回答
1

fwiw,由于构造函数中的一些自定义初始化尝试访问空对象,我收到此错误。

于 2017-09-06T10:14:40.707 回答
0

我在扩展 TextEdit 时遇到了同样的问题。对我来说,错误是我没有在构造函数中添加“public”。在我的情况下,即使我只定义一个构造函数,即带有参数的构造函数ContextAttributeSet. 连线的事情是,只有当我构建一个 APK(无论是否烧录)并将其传输到设备时,该 bug 才会显示出来。当应用程序在 USB 连接设备上通过 AndroidStudio -> RunApp 运行时,该应用程序可以运行。

于 2017-04-19T10:07:21.477 回答
0

就我而言,我添加了这样的循环资源:

<drawable name="above_shadow">@drawable/above_shadow</drawable>

然后改为

<drawable name="some_name">@drawable/other_name</drawable>

它奏效了

于 2017-09-03T12:03:01.797 回答
0

就我而言,我从其他地方复制了我的课程,并没有立即注意到这是一个abstract课程。你不能夸大抽象类。

于 2018-05-24T08:15:02.703 回答
0

这里要理解的是:

ViewClassName(Context context, AttributeSet attrs )通过 xml 对 customView 进行膨胀时调用构造函数。您看到您没有使用 new 关键字来实例化您的对象,即您没有这样做new GhostSurfaceCameraView()。这样做正在调用第一个构造函数,即public View (Context context)

而当从 XML 膨胀视图时,即使用setContentView(R.layout.ghostviewscreen);或使用时findViewById,你,不,不是你!android系统调用ViewClassName(Context context, AttributeSet attrs )构造函数。

阅读文档时这一点很清楚:“从 XML 膨胀视图时调用的构造函数。” 见:https://developer.android.com/reference/android/view/View.html#View(android.content.Context,%20android.util.AttributeSet)

因此,永远不要忘记基本的多态性,永远不要忘记通读文档。它可以节省大量的头痛。

于 2019-11-03T12:41:51.830 回答