Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android text styling error - Resource is not a ColorStateList

I have a very simple textview:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="20sp"
android:textColor="@style/basic_text_color"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />

Which @style/basic_text_color is a color that I wanted to reuse a lot of time in the app, so I make a app_colors.xml under values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--  Text colors style-->
    <style name="basic_text_color" parent="@android:style/TextAppearance">
         <item name="android:textColor">#ff932e</item>
    </style>

    <!-- some other color styles-->
</resources>

The app compile successfully, but then whenever it executed it will hang and following error would be prompted in LogCat:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aaa/com.example.aaa.activity.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class TextView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
    at android.app.ActivityThread.access$600(ActivityThread.java:140)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4898)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class TextView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
    at com.example.aaa.activity.MainActivity.onCreate(MainActivity.java:54)
    at android.app.Activity.performCreate(Activity.java:5206)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
    ... 11 more
Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x1/d=0x7f0b0124 a=-1 r=0x7f0b0124}
    at android.content.res.Resources.loadColorStateList(Resources.java:2068)
    at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
    at android.widget.TextView.<init>(TextView.java:896)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:44)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:40)
    at android.support.v7.internal.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
    at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:806)
    at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:836)
    at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
    ... 22 more

So it looks like the the error here is "Resource is not a ColorStateList (color or path): TypedValue{t=0x1/d=0x7f0b0124 a=-1 r=0x7f0b0124}" ...

Did I styled the font color in a wrong way? Or any other things go wrong? Thanks!

like image 556
passer Avatar asked Jul 27 '15 12:07

passer


2 Answers

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="basic_text_color">#ff932e</color> 
</resources>

layout

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textSize="20sp"
    android:textColor="@colors/basic_text_color"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title" />

You have to create a color resource file if you want to change the textColor, as it was in your question you are trying to set as color a style.

like image 89
gmetax Avatar answered Nov 15 '22 10:11

gmetax


You probably meant to use:

style="@style/basic_text_color"
like image 22
Simas Avatar answered Nov 15 '22 08:11

Simas