Android 膨胀自定义按钮类时出错。NoSuchMethodException

Android 膨胀自定义按钮类时出错。NoSuchMethodException,android,android-custom-view,Android,Android Custom View,我正在尝试创建一个带有两个文本字段的自定义按钮。当我从代码中创建一个实例时,它是有效的。当我尝试从XML膨胀时,它崩溃了 到目前为止,我为解决此问题所做的努力: 遵循并理解SDK中的LabelView代码示例 遵循并理解此优秀的Devxx定制演示文稿 学习并理解了大量有关该主题的教程 花了好几个小时来复习关于stackoverflow的类似问题 将代码剥离回只将属性集传递给超类的基础 从我读到的问题来看,最常见的原因是没有定义接受属性集的所需构造函数,即 MyClass(Context co

我正在尝试创建一个带有两个文本字段的自定义按钮。当我从代码中创建一个实例时,它是有效的。当我尝试从XML膨胀时,它崩溃了

到目前为止,我为解决此问题所做的努力:

  • 遵循并理解SDK中的LabelView代码示例
  • 遵循并理解此优秀的Devxx定制演示文稿
  • 学习并理解了大量有关该主题的教程
  • 花了好几个小时来复习关于stackoverflow的类似问题
  • 将代码剥离回只将属性集传递给超类的基础
从我读到的问题来看,最常见的原因是没有定义接受属性集的所需构造函数,即

MyClass(Context context, AttributeSet attrs) {
    super(context, attrs);
}
在LogCat I中错误列表的末尾,我有:

Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="TitledValueButton">
        <attr name="titleText" format="string" />
    </declare-styleable>

</resources>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.soundconception.custombuttontest2"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

        <com.soundconception.custombuttontest2.TitledValueButton 
            android:id="@+id/test_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1..2..3.."
            app:titleText="Testing" />

</RelativeLayout>
LogCat

threadid=1: thread exiting with uncaught exception (group=0x41a5a700)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soundconception.custombuttontest2/com.soundconception.custombuttontest2.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class com.soundconception.custombuttontest2.TitledValueButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.soundconception.custombuttontest2.TitledValueButton
at android.view.LayoutInflater.createView(LayoutInflater.java:603)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
at android.app.Activity.setContentView(Activity.java:1895)
at com.soundconception.custombuttontest2.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
... 11 more
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructorOrMethod(Class.java:423)
at java.lang.Class.getConstructor(Class.java:397)
at android.view.LayoutInflater.createView(LayoutInflater.java:568)
... 22 more
threadid=1:线程以未捕获异常退出(组=0x41a5a700)
致命异常:主
java.lang.RuntimeException:无法启动活动组件信息{com.soundconcept.custombuttontest2/com.soundconcept.custombuttontest2.MainActivity}:android.view.InflateException:二进制XML文件行#13:膨胀类com.soundconcept.custombuttontest2.TitledValueButton时出错
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)上
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)上
在android.app.ActivityThread.access$600(ActivityThread.java:141)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
位于android.os.Handler.dispatchMessage(Handler.java:99)
位于android.os.Looper.loop(Looper.java:137)
位于android.app.ActivityThread.main(ActivityThread.java:5103)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:525)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:737)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
在dalvik.system.NativeStart.main(本机方法)
原因:android.view.InflateException:二进制XML文件行#13:膨胀类com.soundconcept.custombuttontest2.TitledValueButton时出错
位于android.view.LayoutInflater.createView(LayoutInflater.java:603)
位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
在android.view.LayoutInflater.inflate(LayoutInflater.java:492)
在android.view.LayoutInflater.inflate(LayoutInflater.java:397)
在android.view.LayoutInflater.充气(LayoutInflater.java:353)
位于com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
位于android.app.Activity.setContentView(Activity.java:1895)
位于com.soundconcept.custombuttonest2.MainActivity.onCreate(MainActivity.java:12)
位于android.app.Activity.performCreate(Activity.java:5133)
位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)上
... 还有11个
原因:java.lang.NoSuchMethodException:[类android.content.Context,接口android.util.AttributeSet]
位于java.lang.Class.GetConstructorMethod(Class.java:423)
位于java.lang.Class.getConstructor(Class.java:397)
在android.view.LayoutInflater.createView(LayoutInflater.java:568)
... 还有22个

缺少此构造函数:

public TitledValueButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

将此构造函数添加到自定义按钮类。

缺少另一个构造函数。添加此项以删除崩溃

记住构造函数必须处于公共模式。

public TitledValueButton(Context context, AttributeSet attrs) {

    super(context, attrs); // This should be first line of constructor 
}


记住构造函数必须处于公共模式。

我在试图在布局中声明非静态内部类(使用美元符号)时收到了相同的错误消息。
public TitledValueButton(Context context, AttributeSet attrs) {

    super(context, attrs); // This should be first line of constructor 
}

只需将
static
keywoard添加到我的类中,它就可以工作。

自定义视图以绘制一个圆。你可以将你的构造函数可见性更改为
public
@Sound Concept:你为MainActivity发布了相同的xml。java,请更正它。你可以尝试将第二个构造函数公开吗?@Pankaj:谢谢,我编辑了帖子有时候我盯着自己的代码看得越久,看到的东西就越少。感谢所有新鲜的眼睛。事实上,这可能意味着缺少了另一个构造器:见下面的Lavkush Agrawal的答案。
public TitledValueButton(Context context, AttributeSet attrs) {

    super(context, attrs); // This should be first line of constructor 
}
public TitledValueButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

}