膨胀自定义按钮类时出错

时间:2014-02-22 13:29:25

标签: android android-button

我在创建自己的自定义Button类时遇到问题。我的Button类看起来像这样:

ChoiceButton.java

public class ChoiceButton extends Button {

    public ChoiceButton(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    protected ChoiceButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }   
    public ChoiceButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}

然后在我的主要活动布局中,我按下了这样的按钮:

activity_main.xml中

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >


<com.example.myapp.ChoiceButton
        android:id="@+id/btnChoice1"
        style="?android:attr/buttonStyleSmall"
        android:layout_marginBottom="10px"
        android:background="@drawable/mybutton_background"
        android:layout_width="145dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center"
        android:text="1" >
    </com.example.myapp.ChoiceButton>    

</RelativeLayout>  

当我尝试运行该应用时,我收到错误Error inflating class com.example.myapp.ChoiceButton

在阅读了类似的主题之后,最常见的错误是缺少构造函数,但我认为我在那里得到了所有这三个。我错过了别的什么吗?

布局xml中的所有属性,如style="?android:attr/buttonStyleSmall",当它是常规Button时,所以我只是将它们留在那里,这可能是错误吗?

这是LogCat跟踪:

02-22 13:25:30.987: E/AndroidRuntime(30355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.GameActivity}: android.view.InflateException: Binary XML file line #31: Error inflating class com.example.myapp.ChoiceButton
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.os.Looper.loop(Looper.java:155)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.ActivityThread.main(ActivityThread.java:5454)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at java.lang.reflect.Method.invokeNative(Native Method)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at java.lang.reflect.Method.invoke(Method.java:511)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at dalvik.system.NativeStart.main(Native Method)
02-22 13:25:30.987: E/AndroidRuntime(30355): Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class com.example.myapp.ChoiceButton
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.view.LayoutInflater.createView(LayoutInflater.java:596)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:363)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.Activity.setContentView(Activity.java:1912)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at com.example.myapp.GameActivity.onCreate(GameActivity.java:45)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.Activity.performCreate(Activity.java:5066)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-22 13:25:30.987: E/AndroidRuntime(30355):    ... 11 more
02-22 13:25:30.987: E/AndroidRuntime(30355): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
02-22 13:25:30.987: E/AndroidRuntime(30355):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at java.lang.Class.getConstructor(Class.java:431)
02-22 13:25:30.987: E/AndroidRuntime(30355):    at android.view.LayoutInflater.createView(LayoutInflater.java:561)

1 个答案:

答案 0 :(得分:4)

您的第二个构造函数仅受保护 - 将其公开并且可以正常工作。