2010-09-17 229 views
171

我正在尝试创建扩展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 

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

回答

346

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

有时最简单的事情可能是一个问题:)很高兴知道这两个参数都用于充气。 – Warpzit 2012-02-20 12:19:43

+3

谢谢!在示例中没有任何地方可以找到关于重载所有构造函数的必要性的提示!你救了我几个小时(几天?)的时间。 – 2012-05-14 15:12:30

+1

非常感谢!错误消息是非常不确定的,这让我难以忍受,他们应该在错误消息中包含原因(缺少构造函数重载)。 – AgentKnopf 2012-06-07 09:46:39

41

@Tim - 两者都不需要构造函数,只有ViewClassName(Context context, AttributeSet attrs)构造是必要的。几小时和几小时的浪费时间之后,我发现了这种痛苦的方式。

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

+0

这很有道理,当我在xml中为其定义属性时,我的自定义TextView始终使用ViewClassName(Context上下文,AttributeSet attrs)构造。如果我在没有在xml文件中定义的情况下对其进行实例化,常规构造函数将仅使用上下文ViewClassName(Context上下文)进行调用。我想知道其他的构造函数是做什么的,并根据这个:http://stackoverflow.com/a/4022916/1505341答案,它应该用于设置视图的基础样式。 – mass 2014-03-10 12:52:57

17

“错误充气类”消息的另一个可能的原因可能是拼写错误的地方在了XML指定的完整包名:打开您的布局XML文件在Eclipse XML编辑器应该突出这个问题

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

+2

这确实是我的应用程序的修复程序。 com.zerokol.views.joystickview 成了 com.zerokol.views.JoystickView 和它的工作! – Andy 2015-03-04 19:21:14

+0

是真的。请仔细检查拼写或尝试使用IDE提供的意图进行工作,只需输入您的软件包名称即可开始工作,并且您的所有有用的班级将显示为意图。 – Khay 2017-02-27 08:50:53

+0

这是我的情况。 – 2017-11-07 10:06:58

1

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

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

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

+0

这与@rmtheis所暗示的非常相似。可能更好地评论他的答案,甚至编辑更多的信息。 – 2017-01-25 20:04:41

0

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

0
在我的情况

我说这样的循环资源:

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

则改为

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

和它的工作

0

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