q9722088 2018-01-01 11:08 采纳率: 54.5%
浏览 1769
已采纳

android 第一次做自定义控件 java.lang.RuntimeException异常

package com.example.q9722.biancheng;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

/**

  • Created by q9722 on 2018/1/1. */

public class test_text extends TextView {
private String mText = "111",mNumber = "222";
private Paint mPaint;
private Rect mNumberRect;

public test_text(Context context, String mText, String mNumber) {
    this(context, null, mText, mNumber);
}

public test_text(Context context, AttributeSet attrs, String mText, String mNumber) {
    this(context, attrs, 0, mText, mNumber);
}

public test_text(Context context, AttributeSet attrs, int defStyleAttr, String mText, String mNumber) {
    super(context, attrs, defStyleAttr);
    this.mText = mText;
    this.mNumber = mNumber;
    init();
}

private void init() {
    mPaint = new Paint();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
    canvas.drawCircle(getWidth()/6*4, getHeight()/4*3,getHeight()/4,mPaint);
    mPaint.setTextSize(18);
    mPaint.getTextBounds(mNumber, 0, mNumber.length(), mNumberRect);
    canvas.drawText(mText, 2 ,getHeight()/5,mPaint);
    canvas.drawText(mNumber, (getWidth()/6*4)-(mNumberRect.width()/2), (getHeight()/4*3)-(mNumberRect.hashCode()/2), mPaint);
}

}

01/01 18:58:49: Launching app
$ adb push C:\Users\q9722\AndroidStudioProjects\BianCheng\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.q9722.biancheng
$ adb shell pm install -r "/data/local/tmp/com.example.q9722.biancheng"
Success

$ adb shell am start -n "com.example.q9722.biancheng/com.example.q9722.biancheng.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 30129 on device xiaomi-mi_5-43ac216f
W/System: ClassLoader referenced unknown path: /data/app/com.example.q9722.biancheng-1/lib/arm64
I/InstantRun: Instant Run Runtime started. Android package is com.example.q9722.biancheng, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.example.q9722.biancheng-1/lib/arm64
W/MIUI-BLOCK-MONITOR: The msg { when=-72ms what=110 obj=AppBindData{appInfo=ApplicationInfo{7c1e14a com.example.q9722.biancheng}} target=android.app.ActivityThread$H planTime=1514804333143 dispatchTime=1514804333215 finishTime=0 } took 3826ms and took 3754ms after dispatch.
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/System: ClassLoader referenced unknown path: /system/app/MiuiContentCatcher/lib/arm64
D/AccessibilityManager: current package=com.example.q9722.biancheng, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=true, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@717395
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@bc3ccaa
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@664d14e
D/ActivityThreadInjector: clearCachedDrawables.
I/Adreno: QUALCOMM build : 620c068, I09d312ff84
Build Date : 08/07/17
OpenGL ES Shader Compiler Version: XE031.09.00.04
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread.
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@c8e9e47
I/Timeline: Timeline: Activity_launch_request time:84494803
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@cd359d1
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@4011c36
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.q9722.biancheng, PID: 30129
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.q9722.biancheng/com.example.q9722.biancheng.MainActivity}: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.example.q9722.biancheng.test_text
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2785)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6321)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.example.q9722.biancheng.test_text
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.q9722.biancheng.test_text
Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor0(Class.java:2204)
at java.lang.Class.getConstructor(Class.java:1683)
at android.view.LayoutInflater.createView(LayoutInflater.java:622)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.q9722.biancheng.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:6847)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2785)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6321)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Application terminated.

  • 写回答

10条回答

  • 朴勇佑 2018-01-02 07:44
    关注
    把这些构造函数都加进去就不会报异常了
    
    public test_text(Context context) {
        super(context);
    }
    
    public test_text(Context context, AttributeSet attrs) {
        super(context,attrs);
    }
    
    public test_text(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context,attrs,defStyleAttr);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))