当我将样式应用于按钮时,应用程序崩溃

时间:2014-03-15 20:16:13

标签: android xml android-layout

以下是Button

的xml代码
<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_marginTop="10dp"
    android:layout_weight="0.166"     
    android:text="1"
    android:onClick="ProcessInput"
    android:gravity="fill"
    android:background="@style/Buttoncomponentstyles" />

这里的新内容是我的文件,其中包含该样式:

 <style name="Buttoncomponentstyles" parent="android:Widget.Button">
  <item name="android:background">@drawable/componentstyles_btn_default_holo_light</item>
 </style>

drawable在正确的位置。 下面是我的 logcat

03-16 01:40:50.790: E/AndroidRuntime(27388): FATAL EXCEPTION: main
03-16 01:40:50.790: E/AndroidRuntime(27388): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: android.view.InflateException: Binary XML file line #144: Error inflating class <unknown>
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.os.Handler.dispatchMessage(Handler.java:107)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.os.Looper.loop(Looper.java:194)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.ActivityThread.main(ActivityThread.java:5371)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at java.lang.reflect.Method.invokeNative(Native Method)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at java.lang.reflect.Method.invoke(Method.java:525)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at dalvik.system.NativeStart.main(Native Method)
03-16 01:40:50.790: E/AndroidRuntime(27388): Caused by: android.view.InflateException: Binary XML file line #144: Error inflating class <unknown>
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.Activity.setContentView(Activity.java:1881)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at com.example.calculator.MainActivity.onCreate(MainActivity.java:43)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.Activity.performCreate(Activity.java:5122)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
03-16 01:40:50.790: E/AndroidRuntime(27388):    ... 11 more
03-16 01:40:50.790: E/AndroidRuntime(27388): Caused by: java.lang.reflect.InvocationTargetException
03-16 01:40:50.790: E/AndroidRuntime(27388):    at java.lang.reflect.Constructor.constructNative(Native Method)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
03-16 01:40:50.790: E/AndroidRuntime(27388):    ... 29 more
03-16 01:40:50.790: E/AndroidRuntime(27388): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f0b0085 a=-1 r=0x7f0b0085}
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.content.res.Resources.loadDrawable(Resources.java:1968)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.view.View.<init>(View.java:3338)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.widget.TextView.<init>(TextView.java:591)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.widget.Button.<init>(Button.java:107)
03-16 01:40:50.790: E/AndroidRuntime(27388):    at android.widget.Button.<init>(Button.java:103)
03-16 01:40:50.790: E/AndroidRuntime(27388):    ... 32 more

这里有什么问题。

2 个答案:

答案 0 :(得分:0)

我认为问题出在以下几行:

android:background="@style/Buttoncomponentstyles"

background accepts drawables,而非风格。以下应该有效:

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_marginTop="10dp"
    android:layout_weight="0.166"     
    android:text="1"
    android:onClick="ProcessInput"
    android:gravity="fill"
    android:background="@drawable/componentstyles_btn_default_holo_light"/>

答案 1 :(得分:0)

这可能是因为您使用自己的风格作为背景可绘制而且系统无法将Style转换为Drawable。更改你的xml:

<Button
    android:id="@+id/button1"
    style="@style/Buttoncomponentstyles"
    android:layout_width="0dp"
    android:layout_marginTop="10dp"
    android:layout_weight="0.166"     
    android:text="1"
    android:onClick="ProcessInput"
    android:gravity="fill" />