selecter.xml 的资源未找到异常

use*_*522 5 android

我遇到了一个问题,我收到此错误...

03-02 19:59:03.539:E / AndroidRuntime(11177):java.lang.RuntimeException:无法启动活动ComponentInfo {com.comp4020.kitchenaid / com.kitchenaid.MainActivity}:android.content.res.Resources $ NotFoundException :文件 res/drawable/main_button.xml 来自可绘制资源 ID #0x7f020003

我不知道为什么会发生这种情况。该资源绝对存在,并且命名正确,没有错误。我唯一能想到的是,我向程序添加了默认字符集中不存在的字符,因此它询问“更改为 UTF-8”,我说是。

我按如下方式分配该资源......

ingButton.setBackgroundResource(R.drawable.main_button);
nextButton.setBackgroundResource(R.drawable.main_button);
prevButton.setBackgroundResource(R.drawable.main_button);
timerButton.setBackgroundResource(R.drawable.main_button);
Run Code Online (Sandbox Code Playgroud)

我也尝试过清洁项目。我不知道发生了什么事。

编辑:这是可绘制的

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:state_pressed="true"
        android:background="@drawable/gradient_pressed"
        />
     <item android:background="@drawable/gradient"/>

</selector>
Run Code Online (Sandbox Code Playgroud)

渐变.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient android:startColor="@color/gradientDark" 
              android:centerColor="@color/gradientDark"
              android:endColor="@color/gradientLight" 
              android:angle="90" />
</shape>
Run Code Online (Sandbox Code Playgroud)

渐变_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient android:startColor="@color/highlightGradientDark" 
              android:centerColor="@color/highlightGradientDark"
              android:endColor="@color/highlightGradientLight" 
              android:angle="90" />

</shape>
Run Code Online (Sandbox Code Playgroud)

hta*_*oya 2

新版本的 Android 解释得更详细一些:

E/AndroidRuntime(5591): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML
file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
Run Code Online (Sandbox Code Playgroud)

问题是选择器在每个项目中具有android:color或在您的情况下android:background而不是android:drawable,更改为前一个可以解决问题。