Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ImageView crashes program

I am making a small app for Android, where I have a RelativeLayout, which amongst other things contains a custom ImageView. In my Java code I have this class:

package com.example.android.helloactivity;


class ArrowImageView extends ImageView {
    public ArrowImageView(Context context) {
        super(context);
    }
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(10,10,10,null);
    }
}

Then in my RelativeLayout xml I have the following:

<RelativeLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="#FFF"
            xmlns:android="http://schemas.android.com/apk/res/android">
    <Button ...... />
    <TextView ......./>

    <com.example.android.helloactivity.ArrowImageView
        android:id="@+id/hello_activity_bearingarrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

When I run my main class (not shown here) then my program crashes. If I omit the xml reference to ArrowImageView, then it does not crash.

Am I referring to my custom class te wrong way, or what is going on?

like image 878
Leif Frederiksen Avatar asked Apr 14 '11 18:04

Leif Frederiksen


1 Answers

When extending the View widgets, if you plan to use them in XML layouts you need to also override the constructors that take the AttributeSet argument.

like image 130
LeffelMania Avatar answered Oct 26 '22 00:10

LeffelMania