2 q9722088 q9722088 于 2018.01.01 19:08 提问

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个回答

yongwoozzang
yongwoozzang   2018.01.02 15: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);
}
caozhy
caozhy   Ds   Rxr 2018.01.01 19:20
yanerqingting
yanerqingting   2018.01.02 00:37

楼主的错误应该是出现在第102行空指针错误

YXTS122
YXTS122   2018.01.02 02:10

楼主这是要自定义TextView,还是要。。。。。。。

weixin_38782401
weixin_38782401   2018.01.02 10:11

测试一下get有没有get到东西,如果没有get到你又set就报错。log一下

morgerton
morgerton   2018.01.02 18:14

楼上说的很对,先找个例子对着敲代码吧

u013648164
u013648164   2018.01.02 19:49

构造函数有问题,几个构造函数参数不要随便改,学自定义View推荐看一看hongyang大神的博客。

qq_37043246
qq_37043246   2018.01.05 09:26

这个问题很简单,你在Activity的XML中引用了这个自定义类,而引用这个自定义类的全路径名称不对,在你XML的第15行

u014649395
u014649395   2018.01.08 10:13

把所有的构造函数加上就行了

chenxu2614
chenxu2614   2018.01.08 14:12

构造函数问题,因为放在XML里面调用,默认的构造函数还是需要的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!