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


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

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

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);
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏程序员宝库

Android屏幕截图,View截图(干货)

9310
来自专栏项勇

笔记69 | Android获取MP3文件的ID3信息工具类

18050
来自专栏积累沉淀

多人聊天室

最近学完网络线程协议 ,因此写了一个用java编写的聊天室 话不多说 效果如图 ? 首先 创建服务器端 package com.yc.server...

77480
来自专栏编程之路

羊皮书APP(Android版)开发系列(十二)Android Socket UDP大文件传输

19740
来自专栏Android干货

RecyclerView添加头部和底部视图的实现

70660
来自专栏码匠的流水账

聊聊AbstractOMSProducer

io/openmessaging/rocketmq/producer/AbstractOMSProducer.java

8710
来自专栏向治洪

android获取状态栏高度

公司项目中使用了popWindow,并且需求指明了popWindow必须是和Activity类似的全屏模式,并且从页面底部弹出! 当popWindow所有设...

41060
来自专栏李蔚蓬的专栏

Binary XML file line #19: Attempt to invoke virtual method 'boolean java.lang.String.equals(java....

APP中需要实现LayoutInflater布局加载器动态加载布局,然而开启程序一运行就闪退。。。

24710
来自专栏向治洪

RecyclerView 实现横向滚动效果

我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有HorizontalScrollView效果和ViewPager来做,不过自从Google大会之后,系...

47160
来自专栏Java与Android技术栈

用kotlin打造简化版本的ButterKnife

大名鼎鼎的 ButterKnife 库相信很多 android 开发者都听过,在 Github 上star的数目已经快15k了,而且很多知名的app都在使用。

22630

扫码关注云+社区

领取腾讯云代金券