简体   繁体   中英

InflateException (Error inflating class) when using custom widget in xml

I created a custom imageview. But I get an InflateException when I try to run this. Can someone help me solve this?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/herinnering_background">
 <be.test.ArrowImageView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/kompas_arrow_car"
  />
</FrameLayout>


package be.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.widget.ImageView;

public class ArrowImageView extends ImageView{

    public ArrowImageView(Context context) {
        super(context);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        Paint paint  = new Paint(Paint.LINEAR_TEXT_FLAG);
        paint.setColor(Color.GREEN);
        paint.setTextSize(12.0F);
        canvas.drawText("Hello World in custom view", 100, 100, paint);
    }
}

I think the problem is that you need to implement a constructor with the AttributeSet because this is the one used by the LayoutInflator:

ImageView(Context context, AttributeSet attrs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM