android 自定义视图未展开

android custom view not inflating

我制作了一个扩展视图的自定义视图 class:

 import android.content.Context;
 import android.graphics.Canvas;
 import android.view.View;

 public class DrawSurfaceView extends View
 {

private ButtonsManager BM;
public DrawSurfaceView(Context context) {
    super(context);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    BM.draw(canvas);
}


}

我把它包含在 xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

        <TextureView
        android:id="@+id/gameSurface"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

                    <com.example.trashedometer.DrawSurfaceView
                android:id="@+id/drawingSurface"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                />

</FrameLayout>

但是在主代码中,当它设置内容视图时,它说它不能扩充我的自定义视图,为什么?

 setContentView(R.layout.activity_main);

您至少需要一个构造函数才能允许 inflation 来自 XML 布局。

public class DrawSurfaceView extends View {
    private ButtonsManager BM;

    public DrawSurfaceView(Context context) {
        this(context, null);
    }

    public DrawSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    ...
}