仅在 HTC Desire HD 上从主题设置 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'
应该让它查看我的 theme.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 向我报告“无法解析属性”中的颜色值“?textColor”我的layout.xml 的图形布局选项卡中的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 上不再出现该错误,但文本颜色将始终保持系统 textColor 默认值。在所有设备上,它都按预期完美运行。
编辑:2011 年 7 月 6 日清晨 基本上,HTC Desire HD 似乎会忽略任何用“?”设置的值。尽管我上次编辑中描述了一半修复,但稍后我在应用程序中使用样式的页面上遇到了错误。
编辑:2011年7月6日下午 通过在Java中粗略地设置文本颜色(取决于主题),我已经解决了一半的问题。但是,这对于使用“样式”的 ListView 中的文本还不起作用。然而,我现在发现,尽管使用了“?textColor”,但程序的其余部分都工作正常!这意味着只有打开屏幕无法工作,表明 HTC Desire HD 没有及早创建破译“?textColor”所需的必要主题或样式! (或类似的东西!)这可能是至关重要的信息!我可以交换代码中的任何内容以使其正常工作吗?
Problem:
My app works fine on most devices, but crashes on HTC Desire HD, (Android 2.2.1). (Edit: Error still exists on 2.3.3)
Exception class - android.content.res.Resources$NotFoundException
.
Source method - Resources.loadColorStateList()
Detail:
I am setting textColor using Themes. I declare the textColor in the TextView in the layout xml file ...
<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"/>
and declare what the textColor for each Theme should be in the resources theme file ...
<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>
If I just hardcode the textColor in my xml file, it works fine, so it seems to be failing to understand that '?textColor'
should make it look at my themes.xml file. Again, this works fine on other devices I've tested on, but fails on the HTC Desire HD.
Error:
The relevant bits of the stack trace are ...
android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
and
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)
Edit: Eclipse 3.5.2 reports to me 'Unable to resolve color value "?textColor" in attribute "textColor"' in the Graphical Layout tab of my layout.xml. However, I can still compile and build, and it still works on all other devices other than HTC Desire HD. The full code is at the bottom of this question.
Edit: 1st July 2011
I have noticed that I have two seperate stack traces (although its only 1 line near the end that varies, i think)...
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
and
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
Edit: 4th July - Full code
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>
Edit: 5th July 2011 I have discovered that if I use 'android:textColorPrimary' rather than 'textColor', the error no longer occurs on HTC Desire HD, but the text color will always remain the system textColor default. On all devices, it works perfectly as expected.
Edit: 6th July 2011 Early Morning Basically, HTC Desire HD seems to ignore any values set with a '?'. Despite the half-fix described in my last edit, I get errors later on in my application on pages where styles are used.
Edit: 6th July 2011 Late Afternoon I have half fixed the problem by crudely setting the text color in Java (dependent on the theme). However, this doesn't yet work for my the text in my ListView which uses a 'style'. However, I have now discovered that all of the remainder of my program works fine, despite using '?textColor'! This implies that only the opening screen fails to work, indicating that the HTC Desire HD does not create the neccesary themes or styles early enough needed to decipher '?textColor'! (or something like that!) This could be crucial information! Could I swap anything around in my code to make it work??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这 ”?”语法是引用当前主题中的样式,你在Manifest中设置你的主题吗?我在您提供的代码中没有看到它,所以我假设如果设置了它,那就在那里完成。 (这可能是另一个 Jim 的评论的原因 - 您的回复表明它没有被设置。)
此外,您已经定义了两次“textColor”属性 - 一次在 theme.xml 中,另一次在 attrs.xml 中。任何设备如何知道您指的是哪一个?因为你用的是“?”语法,系统应该知道寻找样式属性,但我认为它不会显式检查资源类型。事实上,有效的语法包括资源类型,“?style/textColor”可能会有所帮助。另外,尝试调用另一个“attrTextColor”以区分它和主题中的“textColor”属性。 (另外,“textColor”在我看来是一个不好的属性名称,因为它可能与属性“android:textColor”混淆 - 也许您应该将其命名为“txtoolsTextColor”或其他名称。因为系统应该知道在您的包中查找对于“textColor”资源,这真的不重要......除了你有一个错误,这可能是原因......)
The "?" syntax is to reference a style in the current theme, do you set your theme in the Manifest? I do not see it in the code you provided, so I assume if it is set, it is done there. (And that is probably the reason for the other Jim's comment - your response indicates it is not being set.)
Additionally, you have defined a "textColor" attribute twice - once in themes.xml and again in attrs.xml. How does any device know which one you are referring to? Because you are using the "?" syntax, the system should know to look for a style attribute, but I don't think it explicitly checks the resource type. In fact, valid syntax includes the resource type, it might help to have "?style/textColor". Also, try calling the other one "attrTextColor" to distinguish between it and the "textColor" attributes in your theme. (Also, "textColor" seems to me like a bad attribute name because it could be confused with the attribute "android:textColor" - maybe you should name it "txtoolsTextColor" or something else. Since the system should know to look in your package for the "textColor" resource, it really shouldn't matter... except that you have a bug and this might be the cause...)
我最近遇到了类似的问题。仅对于 Galaxy S 4G,我在膨胀视图时收到以下错误:
此错误代码非常无用,事实证明查看堆栈跟踪的顶部更有用,我在其中发现:
然后查看 XML在第 804 行有问题的文件中,我发现设置 android:cacheColorHint 存在问题。虽然我确实有一个自定义主题,但它并不适用于我的 ListView,并且 cacheColorHint 只是硬编码的。修复很简单,如上面在 java 中动态设置它:listview.setCacheColorHint。
似乎 java 中的某些视图变量而不是 XML 中的问题通常会减少,平铺位图存在一个错误,也可以通过动态查看图块属性来解决,而不是在 java 中(请参阅 XML 可绘制位图tileMode 错误?)
看起来,对于某些型号的手机和布局,布局在所有操作之前就已经膨胀了。 XML 的内容已被解码。但这只是猜想,或者至少是解决这些问题的一种启发式方法。
I came up against a similar problem recently. For only the the Galaxy S 4G I was getting the following error when inflating a view:
This error code is quite unhelpful, it turned out to be more useful to look at the top of stack trace, where I found:
Then looking at the XML file in question at line 804 I found it was a problem with setting android:cacheColorHint. Although I do have a custom theme, it is not applied to my ListView and cacheColorHint was simply hardcoded. The fix was simple, as above set it dynamically in java: listview.setCacheColorHint.
It does seem that certain view variables in java rather than XML often leads to less problems, there is a bug with tiled bitmaps that is also solved by seeting the tile property dynamicallyr ather than in java (see XML drawable Bitmap tileMode bug?)
It does seem as if, for certain models of phone and layouts, the layout is inflated before all of the XML has been decoded. But this is conjecture, or at least a heuristic way of approaching these problems.
您在评论中提到您正在对 Activity 调用 setTheme,而不是在清单中的 Activity 上设置主题。请确保您在该 Activity 的 onCreate 生命周期方法中的 super.onCreate 调用和 setContent 调用之前调用 setTheme。这将防止在您的主题之前使用任何默认主题。
我怀疑发生的情况是您在布局 XML 中使用了“?textColor”,但该值仅在您的自定义主题和某些设备上的默认主题中定义。其他设备在默认主题中没有定义该值。
错误消息是放入其中的一些值,最终解析为无法用作文本颜色的资源。文本颜色通常是一个 XML 状态列表,它指定用于某些状态的颜色,例如聚焦,但如果您希望所有状态使用相同的颜色,有时会使用直接颜色资源。
就像我在另一条评论中提到的那样,我见过的大多数主题都没有直接的 textColor 值。相反,它们有一个textViewStyle,它指向一个样式,然后有一个textColor。如果你想影响更多的东西,你可能想尝试 colorForeground。通常,textViewStyle 将为 textColor 指定一个 XML 状态列表文件,然后该文件将使用 colorForeground 作为某些状态的颜色。
You mention in your comments that you are calling setTheme on the Activity rather than setting the theme on the activity in the manifest. Please make sure you are calling setTheme before the super.onCreate call and the setContent call in that Activity's onCreate life cycle method. This will prevent any default theme from being used before your theme.
What I suspect is happening is that you are using "?textColor" in your layout XML, but this value is only definied in your custom themes and in the default themes on some devices. Other devices do not have a defined value for that in the default theme.
The error messages are some value getting put in there that ends up resolving to a resource that cannot be used as a text color. The text color is usually an XML state list that specifies the color to use for certain states, like focused, although if you want the same color for all states a direct color resource is sometimes used.
Like I mentioned in another comment, most themes I've seen do not have a textColor value directly. Instead they have a textViewStyle that points to a style that then has a textColor. You might want to try colorForeground if you want to affect more things. Often the textViewStyle will specify an XML state list file for the textColor which will then use the colorForeground for the color for certain states.
该片段不是必需的。主题应该生效,而无需在布局中的任何位置显式指定样式。
This fragment isn't necessary.. the theme should take effect without explicitly specifying the style in your layout anywhere.
我最终选择通过重新处理一些 xml 和 Java 文件来解决这个问题。这感觉并不理想,因为它会在代码中造成不一致,并使主题要简化的东西变得过于复杂。然而,为了迎合HTC Desire HD用户,我已经屈服了。如果有人可以找到替代解决方案,请将其发布在这里。
宽泛地说,我通过在 Java 静态方法中设置文本颜色(例如此)
或通过在 styles.xml 中创建各种样式(每种文本颜色一个)并在 Java 代码中调用相关样式来修复我的代码。
I have eventually opted to fix this issue by reworking some of my xml and Java files. This does not feel ideal, for it creates inconsistency within the code, and overcomplicates something which themes are there to simplify. However, to cater for HTC Desire HD users, I have succumbed to it. If anybody can find an alternative solution, please post it here.
Loosely speaking, I fixed my code by setting text colours within Java static methods such as this...
or by creating various styles within styles.xml (one for each text color), and calling the relevant one in the Java code.