I'm trying to create a custom view GhostSurfaceCameraView
that 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.
谁能告诉我为什么会有这样的错误吗?谢谢。
9 个解决方案
#1
351
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();
}
#2
43
@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.
两个构造函数都不是必需的,只有ViewClassName(上下文上下文、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 View
class 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开发,但我在这里胡乱猜想,这可能是因为,自从我们在XML文件中添加自定义视图类,我们设置多个属性的XML,这需要处理的实例化。但是,一个比我知识渊博得多的人将能够更清楚地阐明这个问题。
#3
17
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文件应该突出这个问题。
#4
2
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中编写完整的类路径是很重要的。当只写了子类的名称时,我就得到了“Error inflating class”。
#5
1
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中添加了自定义视图lib作为一个模块,但是我忽略了将它添加到应用程序的build.gradle中。
dependencies {
...
compile project(':gifview')
}
#6
0
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 Context
and 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也是同样的问题。对我来说,错误是我没有将“公共”添加到构造函数中。在我的例子中,即使我只定义了一个构造函数,一个带参数上下文和AttributeSet的构造函数,它仍然可以工作。最重要的是,只有当我构建一个APK(不管它是否被修改)并将其传输到设备时,这个bug才会显露出来。当应用程序通过AndroidStudio -> RunApp在USB连接设备上运行时,应用程序就会运行。
#7
0
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
这工作
#8
0
fwiw, I received this error due to some custom initialization within the constructor attempting to access a null object.
fwiw,由于构造函数中某些自定义初始化试图访问空对象,我收到了这个错误。
#9
0
In my case, I copied my class from somewhere else and didn't notice right away that it was an abstract
class. You can't inflate abstract classes.
在我的例子中,我从其他地方复制了我的类,并没有立即注意到它是一个抽象类。你不能夸大抽象类。
#1
351
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();
}
#2
43
@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.
两个构造函数都不是必需的,只有ViewClassName(上下文上下文、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 View
class 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开发,但我在这里胡乱猜想,这可能是因为,自从我们在XML文件中添加自定义视图类,我们设置多个属性的XML,这需要处理的实例化。但是,一个比我知识渊博得多的人将能够更清楚地阐明这个问题。
#3
17
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文件应该突出这个问题。
#4
2
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中编写完整的类路径是很重要的。当只写了子类的名称时,我就得到了“Error inflating class”。
#5
1
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中添加了自定义视图lib作为一个模块,但是我忽略了将它添加到应用程序的build.gradle中。
dependencies {
...
compile project(':gifview')
}
#6
0
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 Context
and 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也是同样的问题。对我来说,错误是我没有将“公共”添加到构造函数中。在我的例子中,即使我只定义了一个构造函数,一个带参数上下文和AttributeSet的构造函数,它仍然可以工作。最重要的是,只有当我构建一个APK(不管它是否被修改)并将其传输到设备时,这个bug才会显露出来。当应用程序通过AndroidStudio -> RunApp在USB连接设备上运行时,应用程序就会运行。
#7
0
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
这工作
#8
0
fwiw, I received this error due to some custom initialization within the constructor attempting to access a null object.
fwiw,由于构造函数中某些自定义初始化试图访问空对象,我收到了这个错误。
#9
0
In my case, I copied my class from somewhere else and didn't notice right away that it was an abstract
class. You can't inflate abstract classes.
在我的例子中,我从其他地方复制了我的类,并没有立即注意到它是一个抽象类。你不能夸大抽象类。