NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet...



学自定义View嘛,刚刚少些个重载构造函数,结果。。。

9125154-1e9d4dcba6fe755c.png

9125154-94c7304075e17446.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的时候,三大领头重载构造函数要好好写好呐,缺了容易出错。
喏,三个补齐给他,就完事了:

9125154-80e58b53db40ba3e.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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌川江雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值