Error styling text in text - resource is not a ColorStateList

I have a very simple text:

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

What style @ style / basic_text_color is the color that I wanted to reuse in the application, so I create app_colors.xml in the 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 application compiles successfully, but then whenever it runs, it freezes and the following error will be requested 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 error here is: "Resource is not ColorStateList (color or path): TypedValue {t = 0x1 / d = 0x7f0b0124 a = -1 r = 0x7f0b0124}" ...

Did I use the font style incorrectly? Or are any other things going wrong? Thanks!

+6
source share
4 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 need to create a color resource file if you want to change the textColor, as it was in your question, which you are trying to set as the color of the style.

+2
source

You probably used:

 style="@style/basic_text_color" 
+1
source

Instead of the style attribute, use the color attribute.

 <color name="basic_text_color">#YOUR COLOR GOES HERE</color> 
0
source

If you want to set your color with Java, you can use something like this:

 myView.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.grey_300))); 
0
source

All Articles