android - 仅在 HTC Desire HD 上从主题设置文本颜色失败

标签 android coding-style themes textcolor

问题:

我的应用程序在大多数设备上运行良好,但在 HTC Desire HD (Android 2.2.1) 上崩溃。 (编辑:错误在 2.3.3 上仍然存在)

异常类 - android.content.res.Resources$NotFoundException

源方法 - Resources.loadColorStateList()

详细信息:

我正在使用主题设置 textColor。我在布局 xml 文件中的 TextView 中声明了 textColor ...

<TextView
    android:id="@+id/accountWarning"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/accountWarning"
    android:textSize="20sp"
    android:layout_marginTop="10dp"
    android:padding="10dp"
    android:textColor="?textColor"/>

并在资源主题文件中声明每个主题的 textColor 应该是什么......

<style name="Theme.TxtoolsDark" parent="Theme">
  <item name="textColor">#FFFFFF</item>
  <item name="autoCompleteTextViewTextColor">#000</item>    
</style>

<style name="Theme.TxtoolsLight" parent="Theme" >
  <item name="textColor">#000</item>
  <item name="autoCompleteTextViewTextColor">#000</item> 
</style>

如果我只是在我的 xml 文件中硬编码 textColor,它工作正常,所以它似乎无法理解 '?textColor' 应该让它查看我的 themes.xml 文件。同样,这在我测试过的其他设备上运行良好,但在 HTC Desire HD 上失败。

错误:

堆栈跟踪的相关位是...

android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>

Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/scrollbar_handle_vertical.9.png from drawable resource ID #0x0: .xml extension required
at android.content.res.Resources.loadColorStateList(Resources.java:1842)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
at android.widget.TextView.<init>(TextView.java:665)

编辑: Eclipse 3.5.2 在我的 layout.xml 的图形布局选项卡中向我报告“无法解析属性“textColor”中的颜色值“?textColor”。但是,我仍然可以编译和构建,它仍然可以在除 HTC Desire HD 之外的所有其他设备上运行。完整的代码在这个问题的底部。

编辑:2011 年 7 月 1 日

我注意到我有两个独立的堆栈跟踪(尽管我认为它只有靠近末尾的 1 行有所不同)...

java.lang.RuntimeException: Unable to resume activity  {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.HomeTabs}: java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3399)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3420)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.access$2300(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5073)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3399)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170)
at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:521)
at android.app.ActivityGroup.onResume(ActivityGroup.java:58)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1260)
at android.app.Activity.performResume(Activity.java:4011)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3389)
... 12 more
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:217)
at android.app.Activity.setContentView(Activity.java:1701)
at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.Home.createHomePage(Home.java:88)
at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome.createHomePage(StandardHome.java:74)
at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.Home.onResume(Home.java:291)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1260)
at android.app.Activity.performResume(Activity.java:4011)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3389)
... 18 more
Caused by: java.lang.reflect.InvocationTargetException
at android.widget.TextView.<init>(TextView.java:355)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
... 33 more
Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/scrollbar_handle_vertical.9.png from drawable resource ID #0x0: .xml extension required
at android.content.res.Resources.loadColorStateList(Resources.java:1842)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
at android.widget.TextView.<init>(TextView.java:665)
... 37 more

java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.HomeTabs}: java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2485)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2506)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1856)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2485)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170)
at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:521)
at android.app.ActivityGroup.onResume(ActivityGroup.java:58)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1242)
at android.app.Activity.performResume(Activity.java:4004)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2475)
... 12 more
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:231)
at android.app.Activity.setContentView(Activity.java:1742)
at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.Home.createHomePage(Home.java:88)
at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome.createHomePage(StandardHome.java:74)
at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.Home.onResume(Home.java:291)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1242)
at android.app.Activity.performResume(Activity.java:4004)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2475)
... 18 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 33 more
Caused by: android.content.res.Resources$NotFoundException: Resource is not a         ColorStateList (color or path): TypedValue{t=0x2/d=0x7f010001 a=-1}
at android.content.res.Resources.loadColorStateList(Resources.java:1822)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
at android.widget.TextView.<init>(TextView.java:693)
at android.widget.TextView.<init>(TextView.java:382)
... 36 more

编辑:7 月 4 日 - 完整代码

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
    </style>

    <style name="Theme.TxtoolsDark" parent="Theme">
      <item name="textColor">#FFFFFF</item>
      <item name="autoCompleteTextViewTextColor">#000</item> 
    </style>

    <style name="Theme.TxtoolsLight" parent="Theme" >
      <item name="textColor">#000</item>
      <item name="autoCompleteTextViewTextColor">#000</item>
    </style>

</resources>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="autoCompleteTextViewTextColor" format="reference|color" />
    <attr name="textColor" format="reference|color" />
</resources>

myLayoutFile.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/ScrollView01" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:scrollbars="vertical" android:scrollbarAlwaysDrawVerticalTrack="true">
    <TextView
        android:id="@+id/accountWarning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/accountWarning"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:textColor="?textColor"/>
    <TextView
        android:id="@+id/haveAccountQuestion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/haveAccountQuestion"
        android:textSize="20sp"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:textColor="?textColor"/>
    <Button
        android:id="@+id/yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:width="80dp"
        android:text="@string/yesHappy"
        android:textSize="15sp"
        android:layout_gravity="center_horizontal"/>
    <Button
        android:id="@+id/no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:width="80dp"
        android:text="@string/noSad"
        android:textSize="15sp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"/>
    <ListView android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"/>
</LinearLayout>
</ScrollView>

编辑:2011 年 7 月 5 日 我发现如果我使用“android:textColorPrimary”而不是“textColor”,HTC Desire HD 上将不再出现错误,但文本颜色将始终保持不变系统文本颜色默认值。在所有设备上,它都能按预期完美运行。

编辑:2011 年 7 月 6 日清晨 基本上,HTC Desire HD 似乎会忽略任何设置为“?”的值。尽管我在上次编辑中描述了半修复,但我稍后在我的应用程序中在使用样式的页面上遇到错误。

编辑:2011 年 7 月 6 日下午晚些时候 通过在 Java 中粗略地设置文本颜色(取决于主题),我已经解决了一半的问题。但是,这还不适用于我的 ListView 中使用“样式”的文本。但是,我现在发现,尽管使用了“?textColor”,但我的程序的所有其余部分都可以正常工作!这意味着只有打开屏幕无法工作,表明 HTC Desire HD 没有足够早地创建必要的主题或样式来破译“?textColor”! (或类似的东西!)这可能是至关重要的信息!我可以交换我的代码中的任何内容以使其工作吗??

最佳答案

您在评论中提到您是在 Activity 上调用 setTheme,而不是在 list 中的 Activity 上设置主题。请确保在该 Activity 的 onCreate 生命周期方法中调用 super.onCreate 和 setContent 之前调用 setTheme。这将防止在您的主题之前使用任何默认主题。

我怀疑发生的事情是您在布局 XML 中使用“?textColor”,但此值仅在您的自定义主题和某些设备的默认主题中定义。其他设备在默认主题中没有定义的值。

错误消息是一些被放入其中的值,最终解析为无法用作文本颜色的资源。文本颜色通常是一个 XML 状态列表,指定用于某些状态的颜色,例如聚焦,但如果您希望所有状态都使用相同的颜色,有时会使用直接颜色资源。

就像我在另一条评论中提到的,我见过的大多数主题都没有直接的 textColor 值。相反,它们有一个指向样式的 textViewStyle,然后该样式具有 textColor。如果你想影响更多的东西,你可能想试试 colorForeground。通常 textViewStyle 会为 textColor 指定一个 XML 状态列表文件,然后使用 colorForeground 作为特定状态的颜色。

关于android - 仅在 HTC Desire HD 上从主题设置文本颜色失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5580290/

相关文章:

java - 让多个联系人显示在我的应用程序中有点困难。

android - 在 Android 上添加日历

c++ - 类成员声明的快捷方式

python - Sublime Text Editor 3 - 主题化 - 更改文档字符串颜色

java - android-将全息主题应用到我的应用程序

gtk - 如何在 GTK+3 中设置标题栏关闭按钮的主题?

java - 应用程序在从 Android Studio 运行时工作正常,但在安装签名的 apk 时崩溃

java - GC 优化 : for vs foreach

c# - 如何将 bool 值一般格式化为是/否字符串?

python - 代码风格 - 'hiding' 其他函数中的函数