Java 扩展类时错误膨胀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3739661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:10:06  来源:igfitidea点击:

Error inflating when extending a class

javaandroidxmlclasssurfaceview

提问by eccentricbiped

I'm trying to create a custom view GhostSurfaceCameraViewthat extends SurfaceView. Here's my class definition file

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

GhostSurfaceCameraView.java:

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

And this is in my ghostviewscreen.xml:

这是在我的 ghostviewscreen.xml 中:

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

Now in the activity I made:

现在在我所做的活动中:

protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ghostviewscreen);
    }
}

When setContentView()gets called, an exception is thrown:

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

Can anyone tell me why I get this error? Thanks.

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

采纳答案by eccentricbiped

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:

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

回答by KVNam

@Tim - Both the constructors are not required, only the ViewClassName(Context context, AttributeSet attrs )constructor is necessary. I found this out the painful way, after hours and hours of wasted time.

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

I am very new to Android development, but I am making a wild guess here, that it maybe due to the fact that since we are adding the custom Viewclass in the XML file, we are setting several attributes to it in the XML, which needs to be processed at the time of instantiation. Someone far more knowledgeable than me will be able to shed clearer light on this matter though.

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

回答by rmtheis

Another possible cause of the "Error inflating class" message could be misspelling the full package name where it's specified in XML:

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

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

Opening your layout XML file in the Eclipse XML editor should highlight this problem.

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

回答by Ionoclast Brigham

I had this error plaguing me for the past few hours. Turns out, I had added the custom view lib as a module in Android Studio, but I had neglected to add it as a dependency in app's build.gradle.

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

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

回答by Mykola

It's important to write full class path in the xml. I got 'Error inflating class' when only subclass's name was written in.

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

回答by Nicola Mingotti

I had the same problem extending a TextEdit. For me the mistake was I did non add "public" to the constructor. In my case it works even if I define only one constructor, the one with arguments Contextand AttributeSet. The wired thing is that the bug reveals itself only when I build an APK (singed or not) and I transfer it to the devices. When the application is run via AndroidStudio -> RunApp on a USB connected device the app works.

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

回答by Evgenii Vorobei

in my case I added such cyclic resource:

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

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

then changed to

然后改为

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

and it worked

它起作用了

回答by Tom Howard

fwiw, I received this error due to some custom initialization within the constructor attempting to access a null object.

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

回答by IsaiahJ

In my case, I copied my class from somewhere else and didn't notice right away that it was an abstractclass. You can't inflate abstract classes.

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

回答by kush

The thing to understand here is that:

这里要理解的是:

The constructor ViewClassName(Context context, AttributeSet attrs )is called when inflating the customView via xml. You see you are not using the new keyword to instantiate your object i.e. you are not doing new GhostSurfaceCameraView(). Doing this youare calling the first constructor i.e. public View (Context context).

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

Whereas when inflating view from XML, i.e. when using setContentView(R.layout.ghostviewscreen);or using findViewById, you, NO, not you!, the android systemcalls the ViewClassName(Context context, AttributeSet attrs )constructor.

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

This is clear when reading the documentation : "Constructor that is called when inflating a view from XML." See: https://developer.android.com/reference/android/view/View.html#View(android.content.Context,%20android.util.AttributeSet)

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

Hence, never forget basic polymorphism and never forget reading through the documentation. It saves a ton of headache.

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