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" />
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…