学自定义View嘛,刚刚少些个重载构造函数,结果。。。
![img_af2f36164487ef08dfba1debee55c2ed.png](https://i-blog.csdnimg.cn/blog_migrate/84269e6750b8724ebc69d904b73709fb.png)
![img_2c1cf405a8ab1251c5601ffefec3a18c.png](https://i-blog.csdnimg.cn/blog_migrate/03d33ff2e1500ecf787b574674b25bab.png)
07-30 03:05:30.862 6924-6924/com.example.canvastest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.canvastest, PID: 6924
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.canvastest/com.example.canvastest.SignalViewTest}: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.canvastest.SignalLightView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.canvastest.SignalLightView
at android.view.LayoutInflater.createView(LayoutInflater.java:616)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.canvastest.SignalViewTest.onCreate(SignalViewTest.java:13)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getConstructor(Class.java:495)
at android.view.LayoutInflater.createView(LayoutInflater.java:580)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.canvastest.SignalViewTest.onCreate(SignalViewTest.java:13)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
07-30 03:05:32.928 6924-6924/com.example.canvastest I/Process: Sending signal. PID: 6924 SIG: 9
’话不多说,上Demo代码:**
package com.example.canvastest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
/**
* Created by 700 on 2018/7/30.
*/
public class SignalLightView extends View {
private int count=0;
public SignalLightView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
}
@Override
protected void onDraw(Canvas canvas) {
if(count<100){
count++;
}else{
count=0;
}
Paint paint=new Paint();
paint.setStrokeWidth(3.0f);
switch(count%4){
case 0:
paint.setColor(Color.GREEN);
break;
case 1:
paint.setColor(Color.RED);
break;
case 2:
paint.setColor(Color.BLUE);
break;
case 3:
paint.setColor(Color.YELLOW);
break;
default:
paint.setColor(Color.WHITE);
break;
}
canvas.drawCircle(80, 160, 50, paint);// 画圆,圆心的坐标(cx,cy)和半径radius
}
}
NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet].....
就是说自定义View的时候,三大领头重载构造函数要好好写好呐,缺了容易出错。
喏,三个补齐给他,就完事了:
![img_0025ae6add0673f1f07bc1ffa4c9f87f.png](https://i-blog.csdnimg.cn/blog_migrate/a61618ee6045137656387f3b2dbc65bc.png)
public SignalLightView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
setFocusable(true);
setFocusableInTouchMode(true);
}
public SignalLightView(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setFocusableInTouchMode(true);
}
public SignalLightView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
}