前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet...

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

作者头像
凌川江雪
发布2018-09-13 13:17:46
1.5K0
发布2018-09-13 13:17:46
举报
文章被收录于专栏:李蔚蓬的专栏李蔚蓬的专栏

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

代码语言:javascript
复制
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代码:**

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

代码语言:javascript
复制
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);
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.07.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档