无法创建 CustomSurfaceView

Can't create CustomSurfaceView

我有一个 CustomSurfaceView。我无法在我的应用程序中创建它。什么问题?我创建了所有结构,但不知道为什么我有错误膨胀 class。这是我的代码:

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: Unable to start activity ComponentInfo{com.example.orientationsensor/com.example.orientationsensor.mainActivity}: android.view.InflateException: Binary XML file line #32: Error inflating class CustomSurfaceView

好的,我有办法解决这个问题。我应该在值中创建 attrs.xml 并这样写:

<declare-styleable name="CustomSurfaceView">

</declare-styleable>

还需要将构造者的隐私更改为 public