4

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

Andy A
  • 4,191
  • 7
  • 38
  • 56

5 Answers5

1

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.

Lance Nanek
  • 6,377
  • 2
  • 24
  • 19
0

This fragment isn't necessary.. the theme should take effect without explicitly specifying the style in your layout anywhere.

 android:textColor="?textColor"
Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • Thanks for your response Jim! It didn't work I'm afraid tho. If i remove that line, the text goes back to the default color. If I remember correctly from when I wrote this a few months back, it needs to declare **"?textColor"** to ensure it uses the Theme for the text color. And of course, it works fine on all devices I've tried it on (if I keep this fragment in). I wonder if I've set things up differently to how you imagine? Any other ideas? Thanks. – Andy A Apr 07 '11 at 13:41
  • (In the above comment, I meant to say that the original code works fine in all *other* devices I've tried it on). – Andy A Apr 08 '11 at 09:24
  • Bump. This is still an issue. Does anyone have any more advice? Thanks. – Andy A Apr 11 '11 at 09:38
  • This issue has still not been resolved. Has anyone come across this too? – Andy A Jun 28 '11 at 10:30
  • Hello Andy. I think We hit the same problem - I have a report that one of our apps is crashing on HTC Desire HD and we use themable textColor as well (though we use style="@style/OurTheme").. I guess you have not yet found solution? I do not have unfortunately access to the failing HD, so I cannot see the stack trace.. But I have to fix the ptoblem too ... Do you happen to have an HD that you can run my app on so that I could see the stack trace and do some more debugging? I could send you the app built in special QA mode of (http://apphance.com) so that I could debug it remotely.. – Jarek Potiuk Jun 29 '11 at 21:56
  • Hi Jarek. My company does have a Desire HD, although I cant get my hands on it until 11th July at the earliest. Nevertheless, its interesting that someone else has possibly got the same issue, so please keep in contact on how you are getting on with this. I will edit my question to display the full stack trace, in case this helps. Do you have a stack trace of your error by clicking 'Errors' at https://market.android.com/publish/Home? – Andy A Jul 01 '11 at 08:21
  • Hi Jarek. I now have a Desire HD a week earlier than expected if u want to send me your app? – Andy A Jul 04 '11 at 12:04
  • Jim - I have added more code to my question. Can you please explain to me how you believe I would get this to work without explicitly writing 'android:textColor="?textColor"', like you stated on April 7th? Thank you. – Andy A Jul 04 '11 at 14:10
  • The way most people use themes, they don't set textColor in the theme like you are doing. Although colorForeground is set there and is similar. Anyway, they set textViewStyle in the theme, and the style specified sets the textColor. All TextView's then use that textColor by default, even if there is no textColor attribute on the TextView in the layout XML and nothing is set on it via Java. Similarly, it is much more common, when you do have to add something in the layout XML explicitly to use a style value, to just set the style attribute there rather than each individual attribute... – Lance Nanek May 31 '12 at 17:42
0

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);  
}
Andy A
  • 4,191
  • 7
  • 38
  • 56
0

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...)

Jim
  • 10,172
  • 1
  • 27
  • 36
  • Hi Jim, cheers for your response! Maybe what I have not made clear is that the theme of the app is chosen by the user. Currently there is a choice of two; a light one and a dark one. The theme is therefore not set in the manifest file, it is chosen by the user on the Preferences screen of the app, and set in Java with code such as this - activity.setTheme(R.style.Theme_TxttoolsLight); In regards to your themes.xml / attrs.xml query, the color of each theme is defined in themes.xml, whilst attrs.xml is needed to declare what attributes are in a theme. attrs.xml doesn't define the color itself. – Andy A Aug 08 '11 at 08:10
0

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:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/scrollbar_handle_vertical.9.png from drawable resource ID #0x0: .xml extension required

This error code is quite unhelpful, it turned out to be more useful to look at the top of stack trace, where I found:

ComponentInfo{com.staircase3.opensignal/com.staircase3.opensignal.library.Tab_Overview}: android.view.InflateException: Binary XML file line #804: Error inflating class </code>

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.

Community
  • 1
  • 1
James_OSM
  • 272
  • 4
  • 5