问题描述
我想在 ImageView
上简单地绘制一个矩形.这是 ImageViewHighlight
类:
I want to simply draw a rectangle over an ImageView
. Here is the ImageViewHighlight
class:
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:
But it crashes and gives me this 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
我该如何解决这个问题?
How can I fix this?
推荐答案
InflateException: Binary XML file line #6: Error inflating class com.example.highlight.ImageViewHighlight
表示错误在ImageViewHighlight中.
InflateException: Binary XML file line #6: Error inflating class com.example.highlight.ImageViewHighlight
indicates the error is in ImageViewHighlight.
NoSuchMethodException
表示缺少方法.
<初始化>[class android.content.Context, interface android.util.AttributeSet]表示缺少的方法是一个以Context和AttributeSet为参数的构造函数.
<init> [class android.content.Context, interface android.util.AttributeSet]
indicates the method missing is a constructor that takes a Context and an AttributeSet as parameters.
=> 你需要用相同的参数调用 super() 来实现这个方法.
=> You need to implement this method by calling the super() with the same arguments.
public ImageViewHighlight(Context context, AttributeSet attrs) {
super(context, attrs);
}
这篇关于带有自定义 View 类的 InflateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!