扩展类时出现错误膨胀

我正在尝试创build一个扩展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()被调用时,抛出一个exception:

 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 

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

我想我明白了为什么这不起作用。 我只为一个参数'context'提供了一个构造函数,当我应该为两个参数'Context,AttributeSet'提供一个构造函数时。 我还需要给build设者公共访问权限。 这是我的修复:

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

@Tim – 这两个构造函数都不是必需的,只有ViewClassName(Context上下文,AttributeSet attrs)构造函数是必需的。 经过几个小时的浪费,我发现了这个痛苦的方式。

我对Android开发很陌生,但是我在这里猜测,这可能是由于我们在XML文件中添加了自定义View类,所以我们在XML中设置了一些属性,需要在实例化时进行处理。 然而,比我更懂事的人,将能够更清楚地了解这个问题。

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

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

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

过去几个小时,我有这个错误困扰着我。 事实certificate,我已经将自定义视build.gradle添加为Android Studio中的模块,但是我忽略了将其作为应用程序的build.gradle的依赖项添加。

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

在xml中编写完整的类path很重要。 当只有子类名称被写入时,我得到'错误膨胀类'。

我有扩展一个TextEdit相同的问题。 对我来说,错误是我没有添加“公共”的构造函数。 在我的情况下,即使我只定义了一个构造函数,即带有参数ContextAttributeSet构造函数,它也能正常工作。 有线的事情是,只有当我build立一个APK(烧与否),我把它转移到设备的错误显示自己。 当应用程序通过USB连接的设备上的AndroidStudio – > RunApp运行该应用程序的工作。

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

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

然后改成

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

它的工作

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