错误夸大自定义按钮 [英] Error inflating custom button class
问题描述
我在创建自己的自定义Button类的问题。我Button类看起来是这样的:
ChoiceButton.java
公共类ChoiceButton扩展了Button { 公共ChoiceButton(上下文的背景下){
超级(上下文);
// TODO自动生成构造函数存根
}
保护ChoiceButton(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
}
公共ChoiceButton(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
}
}
然后在我的主要活动的布局,我把我的按钮,像这样的:
activity_main.xml中
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#000000
机器人:方向=垂直>
< com.example.myapp.ChoiceButton
机器人:ID =@ + ID / btnChoice1
风格=机器人:ATTR / buttonStyleSmall
机器人:layout_marginBottom =10px的
机器人:背景=@绘制/ mybutton_background
机器人:layout_width =145dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =CENTER_HORIZONTAL |中心
机器人:文字=1>
< /com.example.myapp.ChoiceButton>< / RelativeLayout的>
当我尝试运行应用程序,我得到的错误错误充气类com.example.myapp.ChoiceButton
阅读类似主题后,最常见的错误是缺少构造函数,但我觉得我得到了他们三个都在那里。我失去了什么东西?
在布局XML中的所有属性,如风格=机器人:ATTR / buttonStyleSmall
在那里当它是一个普通按钮,所以我只是他们留在那里,可以是这样的错误?
下面是LogCat中跟踪:
02-22 13:25:30.987:E / AndroidRuntime(30355):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myapp / com。示例。 myapp.GameActivity}:android.view.InflateException:二进制XML文件行#31:错误充气类com.example.myapp.ChoiceButton
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.ActivityThread.access $ 600(ActivityThread.java:151)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1331)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.os.Handler.dispatchMessage(Handler.java:99)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.os.Looper.loop(Looper.java:155)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.ActivityThread.main(ActivityThread.java:5454)
02-22 13:25:30.987:E / AndroidRuntime(30355):在java.lang.reflect.Method.invokeNative(本机方法)
02-22 13:25:30.987:E / AndroidRuntime(30355):在java.lang.reflect.Method.invoke(Method.java:511)
02-22 13:25:30.987:E / AndroidRuntime(30355):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1029)
02-22 13:25:30.987:E / AndroidRuntime(30355):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
02-22 13:25:30.987:E / AndroidRuntime(30355):在dalvik.system.NativeStart.main(本机方法)
02-22 13:25:30.987:E / AndroidRuntime(30355):android.view.InflateException:二进制XML文件行#31:错误充气类com.example.myapp.ChoiceButton产生的原因
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.view.LayoutInflater.createView(LayoutInflater.java:596)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-22 13:25:30.987:E / AndroidRuntime(30355):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:363)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.Activity.setContentView(Activity.java:1912)
02-22 13:25:30.987:E / AndroidRuntime(30355):在com.example.myapp.GameActivity.onCreate(GameActivity.java:45)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.Activity.performCreate(Activity.java:5066)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-22 13:25:30.987:E / AndroidRuntime(30355):... 11个
02-22 13:25:30.987:E / AndroidRuntime(30355):java.lang.NoSuchMethodException:产生的原因<&初始化GT; [类android.content.Context,接口android.util.AttributeSet]
02-22 13:25:30.987:E / AndroidRuntime(30355):在java.lang.Class.getConstructorOrMethod(Class.java:460)
02-22 13:25:30.987:E / AndroidRuntime(30355):在java.lang.Class.getConstructor(Class.java:431)
02-22 13:25:30.987:E / AndroidRuntime(30355):在android.view.LayoutInflater.createView(LayoutInflater.java:561)
您第二个构造函数只保护 - 把它公开,它会工作。
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)
Your 2nd constructor is only protected - make it public and it will work.
这篇关于错误夸大自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!