我btn_solve_selector.xml
在 Drawable 文件夹中有文件,以便以Button
编程方式设置背景。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff0000ff"/> <!-- default -->
</selector>
使用上面的代码,我在下面的代码中onCreate
编码MyActivity
。
_button1.setBackgroundResource(R.drawable.btn_solve_selector);
但是该应用程序因随后的堆栈跟踪而崩溃。
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_solvenow_selector.xml from drawable resource ID #0x7f020085
....
at com.MyPackage.MyActivity.setMode(MyActivity.java:343)
....
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
....
似乎可绘制的 xml 文件无法读取,即使文件<item />
在其中,然后回退到读取 android.content.res 作为第二次试验。(我什至检查过com.MyPackage.R.drawable.btn_solve_selector
但失败了)。
我删除并重新安装了该应用程序,但失败了。我错过了什么?