【问题标题】:Error inflating when extending a class扩展类时膨胀错误
【发布时间】:2011-04-13 23:17:59
【问题描述】:

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

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

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

【问题讨论】:

    标签: java android xml class surfaceview


    【解决方案1】:

    我想我知道为什么这不起作用了。当我应该为两个参数 '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();
            }
    

    【讨论】:

    • 有时最简单的事情可能会成为问题 :) 很高兴知道这两个参数都用于膨胀。
    • 谢谢!!在示例中,我找不到任何关于重载所有构造函数的必要性!你为我节省了几个小时(几天?)的时间。
    • 非常感谢!错误消息非常不具体,这让我一时难过,他们应该在错误消息中包含原因(缺少构造函数重载)。
    • 感谢您。有谁知道这是否全面用于自定义视图?每当您制作自定义视图时,您都需要包含这两个构造函数吗? (上下文,然后是上下文和属性)
    • 哦,早该看到的! View is not using the 2- **OR** 3-argument View constructors 的消息有点误导。
    【解决方案2】:

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

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

    【讨论】:

    • 这是有道理的,当我在 xml 中为其定义属性时,我的自定义 TextView 总是使用 ViewClassName(Context context, AttributeSet attrs) 构建的。如果我在没有在 xml 文件中定义的情况下实例化它,则仅使用上下文 ViewClassName(Context context) 调用常规构造函数。我想知道其他构造函数做了什么,根据这个:stackoverflow.com/a/4022916/1505341 答案,它应该用于设置视图的基本样式。
    【解决方案3】:

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

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

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

    【讨论】:

    • 这确实是我的应用程序的修复。 com.zerokol.views.joystickview 变成了 com.zerokol.views.JoystickView 并且成功了!
    • 真的。仔细检查拼写或尝试使用您的 IDE 提供的意图,只需输入您的包名称,所有可用的类都将显示在意图下。
    • 这是我的情况。
    【解决方案4】:

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

    【讨论】:

    • 这与@rmtheis 的建议非常相似。可能更好地评论他的答案,甚至使用其他信息对其进行编辑。
    【解决方案5】:

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

    dependencies {
        ...
        compile project(':gifview')
    }
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

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

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

          然后改成

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

          成功了

          【讨论】:

            【解决方案9】:

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

            【讨论】:

              【解决方案10】:

              这里要理解的是:

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

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

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-09-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-08-14
                • 2021-07-19
                相关资源
                最近更新 更多