Android text styling error - Resource is not a Col

2019-02-17 06:27发布

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!

4条回答
相关推荐>>
2楼-- · 2019-02-17 07:04

You probably meant to use:

style="@style/basic_text_color"
查看更多
贼婆χ
3楼-- · 2019-02-17 07:07

If you want to set your color from Java, you could use something like this:

myView.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.grey_300)));
查看更多
等我变得足够好
4楼-- · 2019-02-17 07:17

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.

查看更多
Animai°情兽
5楼-- · 2019-02-17 07:25

You should use the color attribute instead of the style one as.

<color name="basic_text_color">#YOUR COLOR GOES HERE</color> 
查看更多
登录 后发表回答