当扩展一个类时出现膨胀错误

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

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

10个回答

372

我想我已经弄清楚为什么这个方法不起作用了。我只提供了一个参数“context”的构造函数,而应该为两个参数的“Context, AttributeSet”情况提供一个构造函数。我还需要将构造函数设为公共访问权限。这是我的修改:

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();
        }

4
有时候最简单的事情也会成为一个问题 :) 很好知道两个参数都被用来膨胀。 - Warpzit
5
谢谢!在这些示例中,我找不到任何关于必须重载所有构造函数的提及!你帮我省了好几个小时(甚至是几天?)的时间。 - SMBiggs
1
非常感谢!错误信息非常不具体,这让我困惑了一会儿,他们应该在错误信息中包含原因(缺少构造函数重载)。 - AgentKnopf
2
哦,早该看到了!消息“View未使用2- OR 3参数View构造函数”有点误导。 - Attacktive
1
我遇到了同样的问题,但是你提供的解决方案对我不起作用。我正在运行 Android 6.0 API 23。还有其他的解决方案吗? - zulkarnain shah
显示剩余3条评论

45

@Tim - 只需要 ViewClassName(Context context, AttributeSet attrs ) 构造函数,两个构造函数都不是必需的。我通过漫长而痛苦的时间才发现了这一点。

我对Android开发非常陌生,但我猜想可能是因为我们在XML文件中添加自定义View类时,在XML中设置了多个属性,需要在实例化时进行处理。比我更有知识的人将能够更清楚地说明这个问题。


这很有道理,当我在xml中为自定义TextView定义属性时,它总是使用ViewClassName(Context context, AttributeSet attrs)构造。如果我在不在xml文件中定义的情况下实例化它,则只调用常规构造函数context,ViewClassName(Context context)。我一直在想另一个构造函数的作用,根据这个答案:https://dev59.com/tXA75IYBdhLWcg3wJFYL#4022916,它应该用于为视图设置基本样式。 - Kerem

19

「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 文件应该会突出显示此问题。


2
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Andy
不要检查拼写或尝试使用IDE提供的意图,只需开始打包名称,所有可用类都将显示在意图下。 - Khay
这是我的情况。 - Banee Ishaque K

2
重要提示:在xml中写全类路径非常重要。如果只写子类名称,则可能会出现“Error inflating class”的错误。

这与@rmtheis提出的建议非常相似。最好是对他的答案进行评论,甚至附加信息进行编辑。 - Ilia Barahovsky

1
我过去几个小时一直被这个错误困扰着。原来,我已经将自定义视图库添加为Android Studio中的一个模块,但我忽略了将其添加为应用程序build.gradle的依赖项。
dependencies {
    ...
    compile project(':gifview')
}

1
< p > < em > fwiw ,我收到了这个错误,是由于构造函数内的一些自定义初始化尝试访问空对象导致的。


0

在我的情况下,我添加了这样的循环资源:

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

然后更改为

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

它起作用了


0

这里需要理解的是:

当通过xml填充customView时,构造函数ViewClassName(Context context, AttributeSet attrs )会被调用。你看,你没有使用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)

因此,永远不要忘记基本的多态性,也不要忘记阅读文档。这可以避免很多麻烦。

0
在我的情况下,我从别处复制了一个类,但并没有立即注意到它是一个抽象类。你不能够膨胀抽象类。

0

我曾经遇到过扩展TextEdit的同样问题。我的错误是没有在构造函数中添加“public”。在我的情况下,即使我只定义一个带有参数“Context”和“AttributeSet”的构造函数,它也可以工作。奇怪的是,这个bug只会在我构建APK(签名或未签名)并将其传输到设备时才会显现出来。当应用程序通过AndroidStudio -> RunApp在USB连接的设备上运行时,应用程序可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接