android.content.res.Resources$NotFoundException - /res/color/file.xml
Asked Answered
A

5

5

I'm trying to implement the Color State List Resources in my android application, following Android developer guide Color State List Resource | Android Developers. So I created two resource files button_text.xml and button_background.xml in the directory res/color/ and referred them in the with @color/button_text and @color/button_background respectively in the activity_main.xml. Here's the layout code:

<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"/>

On running the App, I'm getting a Resources Not Found Exception in the logs as shown below:

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)

I've tried to clean and rebuild the project but it didn't work. Also, putting the xml files in res/drawable/ instead of res/color/ is showing the same error.Please help solving the issue. Thanks in advance!

EDIT:

Here is the button_text.xml file code:

<?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>

And, button_background.xml file code:

<?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>
Antepast answered 14/6, 2017 at 10:53 Comment(10)
paster your android:textColor="@color/button_text" android:background="@color/button_background" filesHeise
have you set color like <color name="button_text">#272c33</color> in you xml fileWeatherboard
@Weatherboard I've added the color/button_text.xml and color/button_background.xml code in the question @NileshRathodAntepast
Just to be safe, you have double checked the name of the file button_background.xml?Dickson
Affirmative. @j.e.grAntepast
@j.e.gr I checked it. The file name button_background.xm is correct. I've posted the answer, please check.Antepast
@Antepast check my ansHeise
@Antepast did your problrm solvedHeise
Yes @Nilesh RathodAntepast
thna please mark my ans as correct thank youHeise
A
11

So, here it is. Please read the full answer. The android:background attribute does not supports the Color State List unlike android:textColor. To make android:background property work, you need State List Drawables. Thus, instead of res/color/ create the file in res/drawable/ directory. Here's the code:

<?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>

Note the use of android:drawable instead of android:color. Android will use the color resource and make a drawable out of it. To finish this off, you need to add the color resources to your res/values/colors.xml file.

For More information : CLICK HERE!

Antepast answered 14/6, 2017 at 11:40 Comment(0)
O
1

Try This

Change

android:background="@color/button_background"

To

android:backgroundTint="@color/button_background"

The above code work fine, if your button_background.xml file put in res/color/

Osier answered 2/9, 2022 at 11:43 Comment(0)
H
0

try this it will help you follow this

Step 1:-> create a button_text.xml in res/color/ directory.

<?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>

Step 2 :-> create a button_selector.xml in res/drawble/ directory.

<?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>

Step3 :- now appy in your button

    <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" />

aks me in case of any query

Heise answered 14/6, 2017 at 11:34 Comment(2)
Thank you for your answer. button_text.xml file is working fine but button_background.xml wasn't. Check my answerAntepast
in button_background you have to create your custom drawable shapes for buttonsHeise
D
0

If you look better the stack trace exception you should find this:

 Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable

You can find the solution here: Error : <item> tag requires a 'drawable' attribute or child tag defining a drawable

Dickson answered 14/6, 2017 at 11:46 Comment(0)
W
-1

Firstly you have to shift your button_text.xml and button_background.xml to drawable directory.

then set like:

android:textColor="@drawable/button_text"
android:background="@drawable/button_background"
Weatherboard answered 14/6, 2017 at 11:6 Comment(1)
That didn't work but thanks! Moreover, I've provided a link in my Question to the Official Android Developer Guide which says that, we should create the selector's xml file in the res/color/ directory.Antepast

© 2022 - 2024 — McMap. All rights reserved.