-1

why my app crashes when I use extended view

Here is my extended view

public class CustomView extends View
{
    CustomView(Context c)
    {
        super(c);
        init(null);
    }

    CustomView(Context c, AttributeSet attr)
    {
        super(c, attr);
        init(attr);
    }

    public void init(AttributeSet attr)
    {

    }

    @Override
    protected void onDraw(Canvas canvas)
    {

        super.onDraw(canvas);

    }

}

This is the layout file

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

         <com.mycompany. LearnCanvas.CustomView
               android:layout_width="match_parent"
               android:layout_height="200dp"/>

</LinearLayout>

When I run my app, it crashes.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
user9308577
  • 13
  • 1
  • 5

1 Answers1

1

You are missing a constructor which is used by xml. Add this constructor

public CustomView(Context c, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}
Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32