Jonathan Vukadinovic
Jonathan Vukadinovic

Reputation: 264

android custom view not inflating

I made a custom view that extends the View 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);
}


}

I included it in the 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>

But in the main code when it sets the content view it says it can't inflate my custom view, why?

 setContentView(R.layout.activity_main);

Upvotes: 0

Views: 411

Answers (1)

Mike M.
Mike M.

Reputation: 39191

You need at least one more constructor to allow inflation from an XML layout.

public class DrawSurfaceView extends View {
    private ButtonsManager BM;

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

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

Upvotes: 2

Related Questions