gpt4 book ai didi

Android 文本样式错误 - 资源不是 ColorStateList

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:09:46 27 4
gpt4 key购买 nike

我有一个非常简单的 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" />

@style/basic_text_color 是一种我想在应用程序中重复使用很多时间的颜色,所以我在 values 文件夹下制作了一个 app_colors.xml:

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

应用程序编译成功,但每次执行时都会挂起,并在 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

所以看起来这里的错误是“资源不是 ColorStateList(颜色或路径):TypedValue{t=0x1/d=0x7f0b0124 a=-1 r=0x7f0b0124}”...

我是否以错误的方式设置了字体颜色样式?还是其他什么地方出了问题?谢谢!

最佳答案

颜色.xml

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

布局

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

如果您想更改 textColor,您必须创建一个颜色资源文件,因为在您的问题中您正试图将样式设置为颜色。

关于Android 文本样式错误 - 资源不是 ColorStateList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com