我想更改按钮颜色,所以我使用了以下选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FFFF00"/>
<item android:state_pressed="true" android:color="#FFFF00"/>
<item>
<shape>
<gradient android:startColor="#ff00ff00" android:endColor="#bb00ff00" android:angle="270" />
<stroke android:width="1dp" android:color="#bb00ff00" />
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>
</item>
</selector>
并像这样使用它
<Button
android:id="@+id/SODEOD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOD/EOD"
android:background="@drawable/firstbuttoncolorstyle"
/>
但这会导致应用程序崩溃
知道为什么会发生这种情况,以及如何解决它,这就是日志猫
05-15 15:25:06.491: E/AndroidRuntime(666): FATAL EXCEPTION: main
05-15 15:25:06.491: E/AndroidRuntime(666): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appnetics/com.appnetics.TryNewLayoutActivity}: android.view.InflateException: Binary XML file line #92: Error inflating class <unknown>
05-15 15:25:06.491: E/AndroidRuntime(666): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.os.Looper.loop(Looper.java:123)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-15 15:25:06.491: E/AndroidRuntime(666): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 15:25:06.491: E/AndroidRuntime(666): at java.lang.reflect.Method.invoke(Method.java:521)
05-15 15:25:06.491: E/AndroidRuntime(666): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-15 15:25:06.491: E/AndroidRuntime(666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-15 15:25:06.491: E/AndroidRuntime(666): at dalvik.system.NativeStart.main(Native Method)
05-15 15:25:06.491: E/AndroidRuntime(666): Caused by: android.view.InflateException: Binary XML file line #92: Error inflating class <unknown>
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
05-15 15:25:06.491: E/AndroidRuntime(666): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-15 15:25:06.491: E/AndroidRuntime(666): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.app.Activity.setContentView(Activity.java:1647)
05-15 15:25:06.491: E/AndroidRuntime(666): at com.appnetics.TryNewLayoutActivity.onCreate(TryNewLayoutActivity.java:24)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-15 15:25:06.491: E/AndroidRuntime(666): ... 11 more
05-15 15:25:06.491: E/AndroidRuntime(666): Caused by: java.lang.reflect.InvocationTargetException
05-15 15:25:06.491: E/AndroidRuntime(666): at android.widget.Button.<init>(Button.java:65)
05-15 15:25:06.491: E/AndroidRuntime(666): at java.lang.reflect.Constructor.constructNative(Native Method)
05-15 15:25:06.491: E/AndroidRuntime(666): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
05-15 15:25:06.491: E/AndroidRuntime(666): ... 24 more
05-15 15:25:06.491: E/AndroidRuntime(666): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/firstbuttoncolorstyle.xml from drawable resource ID #0x7f020001
05-15 15:25:06.491: E/AndroidRuntime(666): at android.content.res.Resources.loadDrawable(Resources.java:1697)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.view.View.<init>(View.java:1885)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.widget.TextView.<init>(TextView.java:327)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.widget.Button.<init>(Button.java:69)
05-15 15:25:06.491: E/AndroidRuntime(666): ... 28 more
05-15 15:25:06.491: E/AndroidRuntime(666): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
05-15 15:25:06.491: E/AndroidRuntime(666): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:165)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
05-15 15:25:06.491: E/AndroidRuntime(666): at android.content.res.Resources.loadDrawable(Resources.java:1694)
05-15 15:25:06.491: E/AndroidRuntime(666): ... 32 more