Android Can';不创建CustomSurfaceView
我有一个自定义界面。我不能在我的应用程序中创建这个。有什么问题吗?我创建了所有的结构,不知道为什么我会错误地膨胀类。这是我的密码:Android Can';不创建CustomSurfaceView,android,xml,surfaceview,Android,Xml,Surfaceview,我有一个自定义界面。我不能在我的应用程序中创建这个。有什么问题吗?我创建了所有的结构,不知道为什么我会错误地膨胀类。这是我的密码: public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder mHolder; Camera mCamera; CustomSurfaceView(Context context) { super(context);
public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
CustomSurfaceView(Context context)
{
super(context);
init();
}
CustomSurfaceView(Context context, AttributeSet attributeSet)
{
super(context, attributeSet);
init();
}
CustomSurfaceView(Context context, AttributeSet attributeSet, int defStyle)
{
super(context, attributeSet, defStyle);
init();
}
public void init()
{
mHolder = getHolder();
mHolder.addCallback(this);
mCamera = Camera.open();
}
public void surfaceCreated(SurfaceHolder holder)
{
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}catch (IOException e)
{
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h )
{
if (mHolder.getSurface() == null)
{
return;
}
try{
mCamera.stopPreview();
}catch (Exception e) {
}
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}catch (IOException e)
{
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if (mHolder.getSurface() == null)
{
return;
}
try{
mCamera.stopPreview();
}catch (Exception e) {
}
mCamera = null;
}
}
我尝试在XML文件中创建这个视图。这是XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".mainActivity"
android:orientation="vertical">
<com.example.orientationsensor.CustomSurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name = "com.example.orientationsensor.CustomSurfaceView"/>
我在创建时出错。Android Studio向我抛出以下错误:
java.lang.RuntimeException:无法启动活动组件信息{com.example.orientationsensor/com.example.orientationsensor.mainActivity}:android.view.InflateException:二进制XML文件行#32:膨胀类CustomSurfaceView时出错
好的,我有一个解决这个问题的办法。我应该在值中创建attrs.xml,并编写以下内容:
<declare-styleable name="CustomSurfaceView">
</declare-styleable>
还需要将承包商的隐私权公开