在xml中使用自定义窗口小部件时出现InflateException(错误膨胀类)

Vin*_*ent 1 android android-widget

我创建了一个自定义的imageview.但是当我尝试运行它时,我收到了InflateException.有人可以帮我解决这个问题吗?

<?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);
    }
}
Run Code Online (Sandbox Code Playgroud)

jpm*_*jpm 10

我认为问题是您需要使用AttributeSet实现构造函数,因为这是LayoutInflator使用的构造函数:

ImageView(Context context,AttributeSet attrs)