带有自定义View类的InflateException

时间:2012-09-10 12:29:35

标签: android imageview

我想在ImageView上画一个矩形。这是ImageViewHighlight类:

public class ImageViewHighlight extends ImageView {
    Paint paint = new Paint();

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.BLUE);
        canvas.drawRect(30, 20, 60, 40, paint);
    }

}

MainActivity

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

XML:

<com.example.highlight.ImageViewHighlight
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/p5" />

但它崩溃并给了我这个logcat:

09-10 15:23:05.320: E/AndroidRuntime(2347): FATAL EXCEPTION: main
09-10 15:23:05.320: E/AndroidRuntime(2347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.highlight/com.example.highlight.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.highlight.ImageViewHighlight
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.os.Looper.loop(Looper.java:137)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.ActivityThread.main(ActivityThread.java:4575)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at java.lang.reflect.Method.invoke(Method.java:511)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at dalvik.system.NativeStart.main(Native Method)
09-10 15:23:05.320: E/AndroidRuntime(2347): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.highlight.ImageViewHighlight
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.view.LayoutInflater.createView(LayoutInflater.java:589)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.Activity.setContentView(Activity.java:1835)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at com.example.highlight.MainActivity.onCreate(MainActivity.java:12)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.Activity.performCreate(Activity.java:4465)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
09-10 15:23:05.320: E/AndroidRuntime(2347):     ... 11 more
09-10 15:23:05.320: E/AndroidRuntime(2347): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
09-10 15:23:05.320: E/AndroidRuntime(2347):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at java.lang.Class.getConstructor(Class.java:431)
09-10 15:23:05.320: E/AndroidRuntime(2347):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
09-10 15:23:05.320: E/AndroidRuntime(2347):     ... 22 more

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

InflateException: Binary XML file line #6: Error inflating class com.example.highlight.ImageViewHighlight表示错误在ImageViewHighlight中。

NoSuchMethodException表示缺少方法。

<init> [class android.content.Context, interface android.util.AttributeSet]表示缺少的方法是一个构造函数,它将Context和AttributeSet作为参数。

=&GT;您需要通过使用相同的参数调用super()来实现此方法。

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