android.content.res.Resources$NotFoundException - /res/color/file.xml
android.content.res.Resources$NotFoundException - /res/color/file.xml
我正在尝试在我的 android 应用程序中实现 颜色状态列表资源 ,遵循 Android 开发人员指南 Color State List Resource | Android Developers。所以我在目录 res/color/
中创建了两个资源文件 button_text.xml
和 button_background.xml
并分别在 activity_main.xml
中用 @color/button_text
和 @color/button_background
引用它们.这是布局代码:
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:layout_marginTop="150dp"
android:layout_gravity="center"
android:textColor="@color/button_text"
android:background="@color/button_background"/>
在 运行 应用程序上,我在日志中得到一个 Resources Not Found Exception
,如下所示:
Process: com.example.myanimation, PID: 29254
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myanimation/com.example.myanimation.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class Button
Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.myanimation:color/button_background with resource ID #0x7f0b0053
Caused by: android.content.res.Resources$NotFoundException: File res/color/button_background.xml from drawable resource ID #0x7f0b0053
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:729)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:575)
at android.content.res.Resources.loadDrawable(Resources.java:854)
at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
at android.view.View.<init>(View.java:4177)
at android.widget.TextView.<init>(TextView.java:710)
at android.widget.Button.<init>(Button.java:109)
at android.widget.Button.<init>(Button.java:105)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:65)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:61)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.myanimation.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
我尝试清理并重建项目,但没有成功。此外,将 xml files
放在 res/drawable/
而不是 res/color/
中显示相同的 error.Please 有助于解决问题。提前致谢!
编辑:
这里是 button_text.xml
文件代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#000"
android:state_pressed="true"/>
<item android:color="#fff"
android:state_activated="true"/>
</selector>
并且,button_background.xml
文件代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#fff"
android:state_pressed="true"/>
<item android:color="#000"
android:state_activated="true"/>
</selector>
首先,您必须将 button_text.xml
和 button_background.xml
转移到 drawable
目录。
然后像这样设置:
android:textColor="@drawable/button_text"
android:background="@drawable/button_background"
试试这个对你有帮助
关注这个
步骤 1:-> 在 res/color/ 目录中创建一个 button_text.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" android:color="@color/colorPrimary"/>
<item android:state_enabled="false" android:color="@color/colorAccent"/>
<item android:state_enabled="true" android:state_pressed="true" android:color="@color/colorPrimaryDark"/>
</selector>
步骤 2 :-> 在 res/drawble/ 目录中创建一个 button_selector.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/disha"/>
<item android:state_enabled="false" android:drawable="@drawable/back"/>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/ic_edit_pen"/>
</selector>
第 3 步:- 现在在您的按钮中应用
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/button_text"
android:background="@drawable/button_selector" />
如有任何疑问,请联系我
所以,就在这里。请阅读完整答案。与 android:textColor
不同,android:background
属性不支持 Color State List
。要使 android:background
属性 正常工作,您需要 State List Drawables
。因此,而不是 res/color/
在 res/drawable/
目录中创建文件。这是代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white"
android:state_pressed="true"/>
<item android:drawable="@color/black"
android:state_activated="true"/>
</selector>
请注意使用 android:drawable 而不是 android:color。 Android 将使用颜色资源并从中制作可绘制对象。要完成此操作,您需要将颜色资源添加到 res/values/colors.xml 文件。
更多信息:CLICK HERE!
如果堆栈跟踪异常看起来更好,您应该会发现:
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
您可以在这里找到解决方案:
Error : <item> tag requires a 'drawable' attribute or child tag defining a drawable
我正在尝试在我的 android 应用程序中实现 颜色状态列表资源 ,遵循 Android 开发人员指南 Color State List Resource | Android Developers。所以我在目录 res/color/
中创建了两个资源文件 button_text.xml
和 button_background.xml
并分别在 activity_main.xml
中用 @color/button_text
和 @color/button_background
引用它们.这是布局代码:
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:layout_marginTop="150dp"
android:layout_gravity="center"
android:textColor="@color/button_text"
android:background="@color/button_background"/>
在 运行 应用程序上,我在日志中得到一个 Resources Not Found Exception
,如下所示:
Process: com.example.myanimation, PID: 29254
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myanimation/com.example.myanimation.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class Button
Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.myanimation:color/button_background with resource ID #0x7f0b0053
Caused by: android.content.res.Resources$NotFoundException: File res/color/button_background.xml from drawable resource ID #0x7f0b0053
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:729)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:575)
at android.content.res.Resources.loadDrawable(Resources.java:854)
at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
at android.view.View.<init>(View.java:4177)
at android.widget.TextView.<init>(TextView.java:710)
at android.widget.Button.<init>(Button.java:109)
at android.widget.Button.<init>(Button.java:105)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:65)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:61)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.myanimation.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
我尝试清理并重建项目,但没有成功。此外,将 xml files
放在 res/drawable/
而不是 res/color/
中显示相同的 error.Please 有助于解决问题。提前致谢!
编辑:
这里是 button_text.xml
文件代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#000"
android:state_pressed="true"/>
<item android:color="#fff"
android:state_activated="true"/>
</selector>
并且,button_background.xml
文件代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#fff"
android:state_pressed="true"/>
<item android:color="#000"
android:state_activated="true"/>
</selector>
首先,您必须将 button_text.xml
和 button_background.xml
转移到 drawable
目录。
然后像这样设置:
android:textColor="@drawable/button_text"
android:background="@drawable/button_background"
试试这个对你有帮助 关注这个
步骤 1:-> 在 res/color/ 目录中创建一个 button_text.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" android:color="@color/colorPrimary"/>
<item android:state_enabled="false" android:color="@color/colorAccent"/>
<item android:state_enabled="true" android:state_pressed="true" android:color="@color/colorPrimaryDark"/>
</selector>
步骤 2 :-> 在 res/drawble/ 目录中创建一个 button_selector.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/disha"/>
<item android:state_enabled="false" android:drawable="@drawable/back"/>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/ic_edit_pen"/>
</selector>
第 3 步:- 现在在您的按钮中应用
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/button_text"
android:background="@drawable/button_selector" />
如有任何疑问,请联系我
所以,就在这里。请阅读完整答案。与 android:textColor
不同,android:background
属性不支持 Color State List
。要使 android:background
属性 正常工作,您需要 State List Drawables
。因此,而不是 res/color/
在 res/drawable/
目录中创建文件。这是代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white"
android:state_pressed="true"/>
<item android:drawable="@color/black"
android:state_activated="true"/>
</selector>
请注意使用 android:drawable 而不是 android:color。 Android 将使用颜色资源并从中制作可绘制对象。要完成此操作,您需要将颜色资源添加到 res/values/colors.xml 文件。
更多信息:CLICK HERE!
如果堆栈跟踪异常看起来更好,您应该会发现:
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
您可以在这里找到解决方案: Error : <item> tag requires a 'drawable' attribute or child tag defining a drawable