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??
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...
public static void setTextColor(TextView tv) {
if (CURRENT_THEME==TXTTOOLS_LIGHT){
tv.setTextColor(Color.BLACK);
}
else if (CURRENT_THEME==TXTTOOLS_DARK){
tv.setTextColor(Color.WHITE);
}
}
or by creating various styles within styles.xml (one for each text color), and calling the relevant one in the Java code.
SimpleCursorAdapter logins;
if (Theme.getTheme()==Theme.TXTTOOLS_LIGHT){
logins = new SimpleCursorAdapter(this, R.layout.username_row_light_theme, c, from, to);
}
else if (Theme.getTheme()==Theme.TXTTOOLS_DARK){
logins = new SimpleCursorAdapter(this, R.layout.username_row_dark_theme, c, from, to);
}