Question

I have problems creating my own custom Button class. My Button class looks like this:

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);
    }
}

Then in my main activity layout i placed my button like this:

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>  

And when I try to run the app, I get the error Error inflating class com.example.myapp.ChoiceButton

After reading about similar topics, the most common mistake is missing constructors, but I think I got all three of them there. Am I missing something else?

All the attributes in the layout xml such as style="?android:attr/buttonStyleSmall" was there when it was a regular Button, so I just left them there, can that be the error?

Here is the LogCat trace:

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)
Was it helpful?

Solution

Your 2nd constructor is only protected - make it public and it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top